Understanding Super Keyword

Purpose:

    Understanding Super Keyword

Program:

class A
{
	int x;	//declare a variable 'x'
	public a()
	{
		System.out.println("Inside constructor");
	}
	void show()
	{
		x=100;    // assign 'x' variable a value 100
		System.out.println(x); //print value of x
	}
}
class B extends A
{
	public b()  //this is a constructor
	{
		super(); // calls constructor of class A
		super.show(); // calls method of class A
		System.out.println(Super.x);  // calls memeber of class A, that is "x" variable
	}
}

Explanation:
Whenever one class inherits another class, you can call

    parent class constructor via “super();”
    parent class method via “super.show();”
    parent class variable via “super.x;”

Output:
Inside constructor
100
100

Advertisements