Inheritance Concepts In Java

in the details.

You may have certain  methods  which must be over ridden by a subclass, so that the sub class will have a meaning. In such a case, there should be some way to ensure that a subclass does, indeed, over ride all necessary methods. Javas’ solution to this problem is the abstract method.

To declare a method abstract use the following general form:

abstract   return-type  method-name(ParameterList);

Remember

ü  abstract method,  does not define method body defined.

Any class that contains one or more abstract methods must also be declared abstract. To declare a

class  abstract simply use the abstract key word in front of the class key word at the beginning of

the class declaration as shown below

abstract class class-name

{

abstract method1(ParameterList);

abstract method2(ParameterList);

//

//

abstract methodN(ParameterList);

}

Restrictions with Abstract Classes

It is illegal to declare a class as both final and abstract, becuase an abstract class is incomplete by it self and relies up on a sub class for implementation.
An abstract class cannot be directly instantiated with the new operator because such objects would be useless as an abstract class is not fully defined.
An abstract class cannot declare abstract constructors.
An abstract class cannot declare static abstract methods.
Any subclass of an abstract class must be either implement all of the abstract methods of the super class or be declared it self as abstract.

Lets’ illustrate this with the help of an example.

/*

A simple demonstration of abstract class

*/

abstract class A

{

abstract void CallMe();

// concrete methods are still allowed in abstract class

void CallMeToo()

{

System.out.println(“This is a fully defined method in abstract class”);

}

}

class B extends A

{

// since call me is an abstract method ic class A, so it is over ridden below

void CallMe()

{

System.out.println(“B’s implementation of CallMe which is an abstract” +

“method in class A”);

}

}

class AbstractClassDemo

{

public static void main(String args[])

{

B Obj1 = new B();

Obj1.CallMe();

Obj1.CallMeToo();

}

}

Remember

ü  Although abstract classes cannot be used to instantiate objects, they can be used to create object references.

ü   Concrete methods, which define the entire body, are still allowed in abstract class.

Although abstract classes cannot be used to instantiate objects, they can be used to create object references.

As we are aware java’s runtime polymorphism is implemented through the use of super class references.Thus it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. This is best illustrated with the example below

/*

using abstract method and classes and creating  a reference to an abstract class so that it

can be used to point to a subclass object. This is best illustrated with the example below

*/

abstract class Figure

{

double dim1,dim2;

Figure(double x,double y)

{

dim1 = x;

dim2 = y;

}

abstract double area();

}

class Rectangle extends Figure

{

Rectangle(double x,double y)

{

super(x,y);

}

double area()

{

return dim1 * dim2;

}

}

class Triangle extends Figure

{

Triangle(double x,double y)

{

super(x,y);

}

double area()

{

return ((float)1/2)*(dim1*dim2);

}

}

class AbstractClassImplementation

{

public static void main(String args[])

{

//            Figure f = new Figure(10,10);//Illegal because abstract classes cannot be

//instantiated

Rectangle r = new Rectangle(9,5);

Triangle t = new Triangle(10,8);

Figure figref;//creating instance variable of Figure with null as value stored.

figref = r;

System.out.println(“area of Rectangle is “+ figref.area());

figref = t;

System.out.println(“area of Triangle is “+ figref.area());

}

}

I am a trainer and developer in java and oracle technoliogies.

you can reach me on [email protected]

Pages: 1 2 3 4 5 6 7