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.
class Super
{
int a = 5, b = 6;
int add()
{
int c = a + b;
return c;
}
}
public class Sub extends Super
{
void show()
{
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("Result:"+add());
}
public static void main(String[] args)
{
Sub obj = new Sub();
obj.show();
}
}
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.
Related Posts:
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Programming MCQs
- Check Programming Books