public class SuperC {
public void sayHello(){
System.out.println("Hello Super");
}
public void sayBye(){
System.out.println("Hello Super");
}
public static void main(String arg[]){
SuperC sup1 = new SuperC();
SuperC sub1 = new SubC();
sup1 = sub1; // super class reference pointing to sub-class object
sup1.sayHello(); // Hello Sub
SuperC sup11 = new SuperC();
SuperC sub11 = new SubC();
sub11 = sup11; // Super class reference type to super-class object
sub11.sayHello(); // Hello Super
SuperC sup2 = new SuperC();
SubC sub2 = new SubC();
sup2 = sub2; // super class reference pointing to sub-class object
sup2.sayHello(); //Hello Sub
//
SuperC sup3 = new SuperC();
SubC sub3 = new SubC();
sub3 = (SubC) sup3; // Exception in thread "main" java.lang.ClassCastException
// sub class reference type pointing to super class object
SuperC sup4 = new SubC();
SubC sub4 = new SubC();
sub4 = (SubC) sup4 ; // Sub class reference pointer pointing to super class reference type
sub4.sayHello(); // Hello Sub
}
}
class SubC extends SuperC{
public void sayHello(){
System.out.println("Hello Sub");
}
}
public void sayHello(){
System.out.println("Hello Super");
}
public void sayBye(){
System.out.println("Hello Super");
}
public static void main(String arg[]){
SuperC sup1 = new SuperC();
SuperC sub1 = new SubC();
sup1 = sub1; // super class reference pointing to sub-class object
sup1.sayHello(); // Hello Sub
SuperC sup11 = new SuperC();
SuperC sub11 = new SubC();
sub11 = sup11; // Super class reference type to super-class object
sub11.sayHello(); // Hello Super
SuperC sup2 = new SuperC();
SubC sub2 = new SubC();
sup2 = sub2; // super class reference pointing to sub-class object
sup2.sayHello(); //Hello Sub
//
SuperC sup3 = new SuperC();
SubC sub3 = new SubC();
sub3 = (SubC) sup3; // Exception in thread "main" java.lang.ClassCastException
// sub class reference type pointing to super class object
SuperC sup4 = new SubC();
SubC sub4 = new SubC();
sub4 = (SubC) sup4 ; // Sub class reference pointer pointing to super class reference type
sub4.sayHello(); // Hello Sub
}
}
class SubC extends SuperC{
public void sayHello(){
System.out.println("Hello Sub");
}
}