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

«
»
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.

advertisement
  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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn