2. Objectives
On completion of this period, you would be able to
learn
• ‘super’ keyword
• Two general uses of super keyword
• Example programs using super keyword
http://improvejava.blogspot.in
3. Details About super
• Whenever a sub class needs to refer to its immediate
super class it can do so by using keyword super
• super keyword has two general uses
• To call super class constructor
• To access a member of super class that is hidden by a
member of sub class
http://improvejava.blogspot.in
4. To Call Super Class Constructors
• A sub class can call a constructor method
defined by its super class as follows
super(parameter-list);
• parameter-list specifies any parameter needed
by the constructor in the super class
• super() must always be the first statement
executed inside a constructor of subclass
http://improvejava.blogspot.in
4
5. To Call Super Class Constructors contd..
• Constructors are not inherited, even though they have
public visibility
• Yet we often want to use the parent's constructor to set
up the "parent's part" of the object
• The super reference can be used to refer to the parent
class, and often is used to invoke the parent's constructor
http://improvejava.blogspot.in
6. To Call Super Class Constructors contd..
• Used to construct the instance variables of both
the subclass and the superclass
• Uses the keyword super to involve the
constructer method of the superclass
http://improvejava.blogspot.in
7. To Call Super Class Constructors contd..
• The keyword super is used subject to the following
conditions
• Super may only be used within a subclass constructor
method
• The call to super class constructer must appear as the
first statement within the subclass constructer
• The parameters in the super call must match the order
and type of the instance variable declared in the super
class
http://improvejava.blogspot.in
8. Example on super class constructor
class Box{
private double width;
private double height;
private double depth;
// construct clone of one object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
http://improvejava.blogspot.in
8
9. Example on using super reference
//Constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box(){
http://improvejava.blogspot.in
10. Example on using super reference Contd..
width = -1; // use -1 to indicate an un initialized box
height = -1;
depth = -1;
}
// constructor used when a cube is created
Box(double len){
width = height = depth = len;
}
http://improvejava.blogspot.in
11. Example on using super reference Contd..
// Compute and return volume
double volume() {
return width * height * depth;
}
}
// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight box
// construct clone of an object
BoxWeight( BoxWeight ob) { // pass object to constructor
http://improvejava.blogspot.in
12. Example on using super reference Contd..
super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight ( double w, double h, double d, double m) {
super(w,h,d); //call super class constructor
weight = m;
}
// default constructor
BoxWeight() {
super();
weight = -1;
}
http://improvejava.blogspot.in
13. Example on using super reference Contd..
// constructor used when cube is created
BoxWeight( double len, double m) {
super(len);
weight = m;
}
}
http://improvejava.blogspot.in
14. Example on using super reference Contd..
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
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();
http://improvejava.blogspot.in
15. Example on using super reference Contd..
vol = mybox2.volume();
System.out.println( “Volume of mybox2 is” + vol);
System.out.println( “Weight of mybox2 is” +mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println( “Volume of mybox3 is” + vol);
System.out.println( “Weight of mybox3 is” +mybox3.weight);
System.out.println();
vol = myclone.volume();
System.out.println( “Volume of myclone is” + vol);
System.out.println( “Weight of myclone is” +myclone.weight);
System.out.println();
http://improvejava.blogspot.in
16. Example on using super reference Contd..
vol = mycube.volume();
System.out.println( “Volume of mycube is” + vol);
System.out.println( “Weight of mycube is” +mycube.weight);
System.out.println();
}
}
http://improvejava.blogspot.in
17. Output of program
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
Volume of mybox3 is -1.0
Weight of mybox3 is -1.0
Volume of myclone is 3000.0
Weight of myclone is 34.3
Volume of mycube is 27.0
Weight of mycube is 2.0
http://improvejava.blogspot.in
17
18. Accessing hidden member of super class
• Second form of super class refers to the super
class of the sub class in which it is used.
super.member
here the member can be either method or
instance variable.
This form of super is most applicable to member
names of a subclass to access hider members
by the same name in the super class.
http://improvejava.blogspot.in
19. Example Program
// using super to overcome name hiding
class A {
int i;
}
// create a subclass by extending class A
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
}
http://improvejava.blogspot.in
20. Example Program Contd..
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(); Out put of the program is
} i in superclass : 1
} i in subclass : 2
http://improvejava.blogspot.in
21. Summary
In this class we have discussed
• super keyword
• Uses of super keyword
• To call super class constructor
• To access a member of superclass hidden by
member of subclass
http://improvejava.blogspot.in
22. Quiz
1. Whenever a sub class needs to refer to its
immediate super class it can do so by using
keyword _______
a) super
b) this
c) new
d) All the above
http://improvejava.blogspot.in
23. Frequently Asked Questions
• What are the uses of super keyword ?
• Explain two general methods of super with
suitable examples.
http://improvejava.blogspot.in
23