Java Program to Access Super Class Method and Instance Variable Using Super Keyword

This is a Java Program to Access the Super Class Method and Instance Variable Using Super Keyword from Sub Class.

Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. The second is
used to access a member of the superclass that has been hidden by a member of a subclass.

Here is the source code of the Java Program to Access the Super Class Method and Instance Variable Using Super Keyword from Sub Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. class Parent
  2. {
  3.    int x = 5;
  4.    void show()
  5.    {
  6.        System.out.println("Method of parent class using Super Keyword");
  7.    }
  8. }
  9. class Child extends Parent
  10. {
  11.    int x = 9;
  12.    void show()
  13.    {
  14.        System.out.println("Instance Variable of Parent class using Super Keyword :"+super.x);
  15.        System.out.println("Instance variable of Child class :"+x);
  16.        super.show();
  17.        System.out.println("Method of Child class ");
  18.    }
  19.    public static void main(String... a)
  20.    {
  21. 	Child ob = new Child();
  22. 	ob.show();
  23.    }
  24. }

Output:

$ javac Child.java
$ java Child
 
Instance Variable of Parent class using Super Keyword :5
Instance variable of Child class :9
Method of parent class using Super Keyword
Method of Child class

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.