Java Class subclass interview coding questions
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
public class A {
public static void main(String[] args) {
// Java Class subclass interview coding questions
//child class override parent method interview questions
AP ap = new B();
ap.display();
// ap.display2();//compile time error,because parent can call child's object only those parent has provided
AP a = new AP();
a.display();
/*
* B b=(B) new AP();// error at run time -- cannot be cast to class b.display();
*/
B b=new B();
b.display();
b.display2();
}
}
class AP {
void display() {
System.out.println("Display A1...");
}
}
class B extends AP {
void display() {
System.out.println("Display B1...");
}
void display2() {
System.out.println("Display 2 B1...");
}
}
Output :-
Display B1...
Display A1...
Display B1...
Display 2 B1...