Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

SUPER KEYWORD

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


SUPER KEYWORD
❖ Whenever a subclass needs to refer to its immediate superclass, it can
do so by use of the keyword super.

❖If your method overrides one of its superclass's methods, you can
invoke the overridden method through the use of the keyword super.

❖You can also use super to refer to a hidden field (although hiding fields
is discouraged).

❖Only the immediate “parentclass’s” data and methods can be accessed

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


SUPER KEYWORD
➢Super has two general forms:-

✓The first calls the superclass' constructor.

✓The second is used to access a member of


the superclass that has been hidden by a member
of a subclass.

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


PROGRAM TO SHOW SUPER KEYWORD

public class Superclass


{
public void printMethod()
{
System.out.println("Printed in Superclass.");
}
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


Here is a subclass, called Subclass, that overrides printMethod():
public class Subclass extends Superclass
{
public void printMethod() // overrides printMethod in Superclass
{
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args)
{
Subclass s = new Subclass();
s.printMethod();
}
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


❖ Within Subclass, the simple name printMethod() refers to the
one declared in Subclass, which overrides the one in Superclass.

❖ So, to refer printMethod() inherited from Superclass, Subclass must use a


qualified name, using super as shown. Compiling and executing Subclass prints
the following:

Printed in Superclass.
Printed in Subclass.

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


A Second Use for super
❖The second form of super acts somewhat like this,
except that it always refers to the superclass of the
subclass in which it is used.
❖This usage has the following general form:
super.member
❖Here, member can be either a method or an
instance variable.
❖This second form of super is most applicable to
situations in which member names of a subclass
hide members by the same name in the superclass.

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


Program
class A {
int i;
}
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


❖This program displays the following:
✓i in superclass: 1
✓i in subclass: 2
❖Although the instance variable i in B hides the i
in A, super allows access to the I defined in the
superclass. As you will see, super can also be used
to call methods that are hidden by a subclass.

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


Superclass constructor
❖ The syntax for calling a superclass constructor is
super();
--or–
super(parameter list);
❖With super(), the superclass no-argument constructor is called.

❖With super(parameter list), the superclass constructor with a matching parameter list is called.

❖If a constructor does not explicitly invoke a superclass constructor, the Java compiler
automatically inserts a call to the no-argument constructor of the superclass.

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


Superclass constructor

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


class Box {
private double width;
private double height;
private double depth;
Box(Box ob) {
// pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
class BoxWeight extends Box {
double weight;
BoxWeight(BoxWeight ob) {
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR


BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
}
}
PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR

You might also like