Super keyword.23

Post on 22-Oct-2014

880 views 0 download

Tags:

description

 

transcript

1

Using super keyword

http://improvejava.blogspot.in

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

http://improvejava.blogspot.in

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

To Call Super Class Constructors

4

• 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

http://improvejava.blogspot.in

• 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

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

To Call Super Class Constructors contd..

http://improvejava.blogspot.in

• 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

To Call Super Class Constructors contd..

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

http://improvejava.blogspot.in

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

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;

}

Example on using super reference Contd..

http://improvejava.blogspot.in

// 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

Example on using super reference Contd..

http://improvejava.blogspot.in

super(ob); weight = ob.weight; }// constructor when all parameters are specifiedBoxWeight ( double w, double h, double d, double m) {

super(w,h,d); //call super class constructor weight = m;}

// default constructor BoxWeight() {super(); weight = -1;}

Example on using super reference Contd..

http://improvejava.blogspot.in

// constructor used when cube is created

BoxWeight( double len, double m) {

super(len); weight = m; }}

Example on using super reference Contd..

http://improvejava.blogspot.in

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();

Example on using super reference Contd..

http://improvejava.blogspot.in

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();

Example on using super reference Contd..

http://improvejava.blogspot.in

vol = mycube.volume();

System.out.println( “Volume of mycube is” + vol);

System.out.println( “Weight of mycube is” +mycube.weight);

System.out.println();

}

}

Example on using super reference Contd..

17

Output of program

Volume of mybox1 is 3000.0Weight of mybox1 is 34.3

Volume of mybox2 is 24.0Weight of mybox2 is 0.076

Volume of mybox3 is -1.0Weight of mybox3 is -1.0

Volume of myclone is 3000.0Weight of myclone is 34.3 Volume of mycube is 27.0Weight of mycube is 2.0

http://improvejava.blogspot.in

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

Example Program

// using super to overcome name hidingclass 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

http://improvejava.blogspot.in

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

Example Program Contd..

Summary

http://improvejava.blogspot.in

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

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

Frequently Asked Questions• What are the uses of super keyword ?

• Explain two general methods of super with suitable examples.

23http://improvejava.blogspot.in