Page 1 of 1

Abstract classes why do you need them? Rate Topic: ***-- 2 Votes

#1 bhandari   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 18 February 2008 - 05:32 AM

Hi All,

Lets quickly get to our lesson:

An abstract class is like a normal class with the difference that it contains at least one abstract method or it extends another abstract class.
Thus the two conditions under which a class is and must be declared as abstract are:
1) It extends another abstract class
2) It declares an abstract method.

The above implies that at some point the hierarchy of abstract classes, one class must have declares an abstract method. So let’s discuss an abstract method. An abstract method looks like:

abstract void foo();

The difference between this and a normal method is that the abstract methods don’t have method body. This means to compiler that the current class is not meaningful in the real world scenario and some other class which will be extending this class will provide complete definition of the abstract method. In a way, this enforces that all subclasses of an abstract classes have to define the method body for abstract method.

An abstract class definition looks like:

abstract class Myfoo { 
	abstract void foo();

Another version of the above abstract class can be:

abstract class Myfoo { 
	abstract void foo();
	void anotherfoo() {
		System.out.println(“I am not a abstract method”);

The above example shows that an abstract class can have some implemented methods in addition to abstract methods.

Now what about the below code:

abstract class foo {
	void Foo { 
		System.out.println(“This is right”);

The compiler will be more than happy in the above case because of the marker abstract before the class. Had the class not been declared as abstract, the compiler would have flashed an error unless an implementation of the abstract method foo(); has not be given in the subclass.

As far as design is concerned, the abstract classes are provided so that some common guidelines can be provided to the subclasses. For e.g. a class named as Figure will have an abstract method as:

abstract void setColor();

which will make sure that Square, Rectangle and Triangle which extend the Figure class will have to provide a method body for the above method.

One more implication of abstract classes is that since abstract classes are meant to be extended, so they can’t be instantiated. However a reference can be declared and is declared quite often in well designed application. For e.g in the Figure class above, a reference to Figure class can hold objects of type Square, Rectangle and Triangle and hence provides more usability.

Please note that final and abstract are mutually exclusive.

The point to be taken home from the above discussion is that declare classes as abstract where generality is implicit and more concrete implementations for this generality follow.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1