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

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.