Consider this below code snippet
public class SuperClass {
public void move()
{
System.out.println("i am in super class");
}
}
public class SubClass1 extends SuperClass{
public void move()
{
System.out.println("i am in sub1 class");
}
}
public class SubClass2 extends SuperClass {
public void move()
{
System.out.println("i am in sub2 class");
}
}
Now i am creating object like this.
public class program {
public static void main(String[] args) {
SubClass1 obj = new SubClass2(); // Compile error - Type mismatch: cannot convert from SubClass2 to SubClass1
obj.move();
}
}
Why i can't do like this ? What stopping me to write this ??
Aucun commentaire:
Enregistrer un commentaire