Up-casting là gì?

Phrase Java

Trong Java, việc chuyển đổi kiểu lớp con (subclass) thành kiểu lớp cha (superclass ) được gọi là up-casting. Xem ví dụ code bên dưới.


class Super {
   void Sample() {
      System.out.println("method of super class");
   }
}

public class Sub extends Super {
   void Sample() {
      System.out.println("method of sub class");
   }
   
   public static void main(String args[]) {
      Super obj =(Super) new Sub(); obj.Sample();
   }
}

Learning English Everyday