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

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

We inherit the class Super in class Sub using the concept of Inheritance. Due to which all the Methods and Instance Variable of class Super (which are not private)are available to class Sub and we can directly use the Methods and Instance Variable without using Super Keyword.

Here is the source code of the Java Program to Access the Super Class Method and Instance Variable Without 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 Super
  2. {
  3.     int a = 5, b = 6;
  4.     int add()
  5.     {
  6.         int c = a + b;
  7.         return c;
  8.     }
  9. }
  10. public class Sub extends Super
  11. {
  12.     void show()
  13.     {
  14.         System.out.println("a:"+a);
  15.         System.out.println("b:"+b);
  16.         System.out.println("Result:"+add());
  17.     }
  18.     public static void main(String[] args)
  19.     {
  20.         Sub obj = new Sub();
  21.         obj.show();
  22.     }
  23. }

Output:

$ javac Sub.java
$ java Sub
 
a:5
b:6
Result:11

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.