Page 368 - CITS - Computer Software Application -TT
P. 368
COMPUTER SOFTWARE APPLICATION - CITS
{
System.out.println(
“This is the method in SubClass”);
return “Hello, World!”;
}
}
public class Test {
public static void main(String[] args)
{
SuperClass obj1 = new SuperClass();
obj1.method();
SubClass obj2 = new SubClass();
obj2.method();
}
}
6 Invoking overridden method from sub-class
We can call the parent class method in the overriding method using the super keyword.
A Java program to demonstrate that overridden method can be called from sub-class
class Parent {
void show() { System.out.println(“Parent’s show()”); }
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override void show()
{
super.show();
System.out.println(“Child’s show()”);
}
}
// Driver class
class Main {
public static void main(String[] args)
{
Parent obj = new Child();
obj.show();
}
}
355
CITS : IT&ITES - Computer Software Application - Lesson 94 - 100