Write a program in Java with class A being super class and class
B and class C are hierarchically inherited form class A. All the classes have
show() function. Call all of them. Use reference Variable.
Program:
class
A
{
void show()
{
System.out.println("Hello A");
}
}
class
B extends A
{
void show()
{
System.out.println("Hello B" );
}
}
class
C extends A
{
void show()
{
System.out.println("Hello C" );
}
}
class
Sk17
{
public static void main(String args[])
{
A ref;
A objA=new A();
B objB=new B();
C objC=new C();
objA.show();
ref=objB;
ref.show();
ref=objC;
ref.show();
}
}
Compile:
D:\>javac
Sk17.java
Run:
D:\>java
Sk17
Output:
Hello
A
Hello
B
Hello
C
Remarks:
1. Property of a default constructor
is called regardless in explicit calling.
2. Here multi-level
inheritance is depicted, so default constructor of class A is called implicitly
den constructor of class B is called implicitly and finally the constructor of
C is called explicitly.
3. In case of default constructor
in multi-level inheritance "super" keyword has no value
0 Comments