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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s