Inheritance Concepts In Java

called”);

}

}

class B extends A

{

int k,l;

B()

{

System.out.println(“Hi The constructor of B class is called”);

}

}

class C extends B

{

int m,n;

C()

{

System.out.println(“Hi The constructor of C class is called”);

}

C(int x,int y)

{

m = x;

n = y;

System.out.println(“Hi The constructor of C class is called value of m and n “+m+” “+n);

}

}

class OrderOfConsDemo

{

public static void main(String args[])

{

C obj1 = new C();

C obj2 = new C(1,2);

}

}

Explanation : observe the out put’s of both objects obj1 and obj2.

Second use of super :

To access members of the super class hidden by members of the subclass. The member could

either be a  variable or a method, of a class. The general form is

super.member

2.1 super used to access hidden member variables of a super class

/*          lets best illustrate the usage of super with an example, shown below         */

//usage of super to over come name hidingf

class A

{

int i;

}

class B extends A

{

int i,j; // this hides the i in class A

B(int x,int y)

{

i = x;

super.i = y;

j = super.i;

}

void show()

{

System.out.println(“value of i in class A  “+super.i+”value of j  “+j);

}

}

class UseSuper

{

public static void main(String args[])

{

B obj = new B(10,20);

System.out.println(“value of i in B class is “+obj.i);

System.out.println(“value of i in A class is “+obj.j);

obj.show();

}

}

Let us bring  polymorhism memories again

We had already discussion about polymorphism in session (DetailedLookAtMethodsAndClasses-2). There we had discussed, in length, about one form of polymorphism: method overloading.

In this session we discuss, method overriding in detail.

2.2   Method Over Riding In a class hierarchy when a method in a sub class and super class, has the same name and type signature(parameters), then the method in subclass, is said to over ride, the method in the super class.

Remember

ü  When an overridden method is called from with in a subclass.The call will always refer to the version of the method defined by the subclass.The version defined by the super class will always be hidden.

lets understand, with an example shown, below.

/*

Method overriding

*/

// method overriding demo

class A

{

int i,j;

A(int x,int y)

{

i = x;

j = y;

}

void display()

{

System.out.println(“Display method of class A is called with values of i and j “+i+” “+j);

}

}

class B extends A

{

int k,l;

B(int x,int y)

{

super(x+10,y+10);

k = x;

l = y;

/* possible to assign values to i and j of class A in constructor of B

i = x + 10;

j = y + 10;

*/

}

void display()

{

System.out.println(“Display method of class B is called with values of K and l “+k+” “+l);

}

}

class MethodOverRideDemo

{

public static void main(String args[])

{

B obj = new B(20,23);

obj.display();//this calls display() method of class B

}

}

Remember

ü  Over ridden methods allow java to support dynamic, run-time polymorphism.

ü  Method over riding occurs only when name and type signatures of the two methods are identical, If they are not then the methods are simply over loaded.

ü   Polymorphism is essential to object oriented programming for one reason, it allows to specify methods in a general class that will be common to all  of its,subclasss'(derivatives),while allowing the subclasses to define specific implementation of some or all of those methods.

2.2.1 Applying Method Over Loading lets examine the benefits of method over riding with the help of  examples below

/*

This program creates a super class called Figure that stores dimensions of various,two

dimensional objects.It also defines a method called area() that computes the area of an

object.The program derives two subclasses from figure.The first is Rectangle and the second

is Triangle.Each of these subclasses overrides area() so that it returns the area of a

rectangle and a triangle, respectively.

*/

class Figure

{

double dim1;

double dim2;

Figure(double x,double

Pages: 1 2 3 4 5 6 7