package test;
public class Test {
public static void main(String[] args) {
A a2 = new B(); // 1
a2.display1();
a2.display();
((B) a2).display2(); // 2
((A) a2).display(); // 3
B b1 = new B();
b1.display1(); // 4
}
}
class A {
int a = 10;
void display() {
System.out.println("parent class " + a);
}
void display1() {
System.out.println("parent class " + (a + 10));
}
}
class B extends A {
int a = 30;
@Override
void display() {
System.out.println("child class " + a);
}
void display2() {
System.out.println("child class " + (a + 10));
}
}
- whose object is created ?class A or B.
- Is this downcasting
- why this is not invoking the method of class A? how to invoke method of class A (override one) with this object and not with the object of A.
- is this implicit upcasting?
- If a class A have a nested Class B than during object creation of class A
does object of class b is also formed ?i was not able to use method of class B from object of A