Test.java
public class Test {
public void go(){
System.out.println("Test go");
}
}
Test2.java
public class Test2 extends Test {
public void go(){
System.out.println("Test 2 go");
}
public void back(){
System.out.println("Test 2 back");
}
}
class Demo{
public static void main(String[] args) {
Test t=new Test2();
t.go(); // Output : "Test 2 go"
t.back(); //Compile time error.
}
}
I read some issues on stackoverflow regarding this but I did not understood the meaning of SuperClass s=new SubClass();
.
Also in output if Object of Test can access the go()
method of Test2 then why it cannot access back()
method.