Program illustrating use of Final and Super keyword in Java.

Program illustrating use of Final and Super keyword in Java.


class Parent 

{

 final String parentName = "Parent";

 final void display() 

 {

  System.out.println("This is the parent class.");

 }

}

class Child extends Parent 

{

 /* Attempting to override the final method will result in a compilation error

 void display() 

 {

  System.out.println("This is the child class.");

 }*/

 void displayChild() 

 {

  System.out.println("This is a method in the child class.");

 }

 void displayParentName() 

 {

  System.out.println("Parent name from child: " + super.parentName);

 }

}

class Test

{

 public static void main(String[] args) 

 {

  Child chl = new Child();

  chl.displayChild();

  chl.displayParentName();

 }

}

Output




 

Post a Comment

0 Comments