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.
class Parent
{
int x = 5;
void show()
{
System.out.println("Method of parent class using Super Keyword");
}
}
class Child extends Parent
{
int x = 9;
void show()
{
System.out.println("Instance Variable of Parent class using Super Keyword :"+super.x);
System.out.println("Instance variable of Child class :"+x);
super.show();
System.out.println("Method of Child class ");
}
public static void main(String... a)
{
Child ob = new Child();
ob.show();
}
}
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 Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Practice Programming MCQs
- Practice Information Technology MCQs
- Apply for Java Internship
- Practice BCA MCQs