Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces

Super Keyword

Syntax with Example Java Programs

Super is a keyword used to access the immediate parent class from subclass. There are three ways by which the keyword super is used.

Super Keyword

Super is a keyword used to access the immediate parent class from subclass. There are three ways by which the keyword super is used.

Let us understand these uses of keyword super with illustrative Java programs.

1.The super() is used to invoke the class method of immediate parent class.

Java Program[B.java]

class A

{

int x=10;

}

class B extends A

{

int x=20;

void display()

{

System.out.println(super.x);

}

public static void main(String args[])

{

B obj =new B();

obj.display();

}

}

Output

10

Program Explanation: In above program class A is a immediate parent class of class B. Both the class A and Class B has variables x. In class A, the value of x variable is 10 and in class B the value of variable x is 20. In display function if we would write

System.out.println(x);

The output will be 20 but if we user super.x then the variable x of class A will be referred. Hence the output is 10.

2. The super() is used to access the class variable of immediate parent class.

Java Program[B.java]

class A

{

void fun()

{

System.out.println("Method: Class A");

}

}

class B extends A

{

void fun()

{

System.out.println("Method: Class B");

}

void display()

{

super.fun();

}

public static void main(String args[])

{

B obj =new B(); obj.display();

}

}

Output

Method: Class A

Program Explanation: In above program, the derived class can access the immediate parent's class method using super.fun(). Hence is the output. You can change super.fun() to fun(). Then note that in this case, the output will be invocation of subclass method fun.

3. The super() is used to invoke the immediate parent class constructor.

Java Program[B.java]

class A

{

A()

{

System.out.println("Constructor of Class A");

}

class B extends A

{

B()

{

super();

System.out.println("Constructor of Class B");

}

public static void main(String args[])

{

B obj=new B();

}

}

Output

Constructor of Class A

Constructor of Class B

Program Explanation: In above program, the constructor in class B makes a call to the constructor of immediate parent class by using the keyword super, hence the print statement in parent class constructor is executed and then the print staternent for class B constructor is executed.

Ex. 2.9.1: What is inheritance? How will you call parameterized constructor and overrided method from parent class in sub class?

Sol. Inheritance: - Inheritance is a mechanism in Java by which derived class can borrow the properties of base class and at the same time the derived class may have some additional properties.

Calling Parameterized constructor from parent class in subclass

class Parentclass

{

//no-arg constructor

Parentclass()

{

System.out.println("no-arg constructor of parent class");

}

//arg or parameterized constructor

Parentclass(String str)

{

System.out.println(str+" This is parameterized constructor of parent class");

}

void display(String str)

{

System.out.println(str+" This is overridden method");

}

}

class Subclass extends Parentclass

{

Subclass()

{

super("Welcome");//calling super class's parameterized constructor

System.out.println("Constructor of child class");

}

void display()//overridden method

{

super.display("Hi"); //calling super class overridden method

}

public static void main(String args[])

{

Subclass obj= new Subclass();

obj.display();

}

}

Output

Welcome This is parameterized constructor of parent class

Constructor of child class

Hi This is overridden method

Review Question

1. With suitable program segments describe the usage of super keyword.


Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : Syntax with Example Java Programs - Super Keyword