Java Program to use This Keyword in Inheritance Class

This is a Java Program to Use Super Keyword in Inheritance Class.

Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.We made two same data member in base and child class and we call the base class data member from child class by using Super Keyword.

Here is the source code of the Java Program to Use Super Keyword in Inheritance Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. class Base 
  2. {
  3.   int x = 19;
  4. }
  5.  
  6. class Child extends Base 
  7. {
  8.     int x = 20;
  9.     void shows() 
  10.     {
  11.         System.out.println("The base class data member (x) by Super Keyword :" + super.x);
  12.         System.out.println("The child class data member :" + x);
  13.  
  14.     }
  15.     public static void main(String... a) 
  16.     {
  17.         Child obj = new Child();
  18.         obj.shows();       
  19.     }
  20. }

Output:

$ javac Child.java
$ java Child
 
The base class data member (x)by Super Keyword :19
The child class data member :20

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.