JAVA – Abstract Classes and Methods

The definition of a class should not be instantiated since it only represents the abstract concept. For example, the instance of pet can not be seen. What you can see are the instances of cat, dog, pig or horse. The instance of pet can not be seen and does not make sense.

Abstract Classes

As with object-oriented programming, declaring abstract concepts might be desired, but there is no possibility that you can create an instance of same. For example, the abstract concept of numbers is represented by Number class available with the java.lang package. The Number class serves as a model for numbers used in programs but is impossible to create the generic object.

The Number class is parent class of number of other number classes like Integer and double which implements specific types of numbers. The abstracted class represented by the number class, which implements abstract concepts. Abstract classes can be subclassed but it cannot have instance of it.

If you are declaring an abstract class, Use keyword ‘abstract’ just before ‘class’ keyword. This tells compiler that you are declaring an abstract class. In syntax form:

abstract class class_name {

. . .

}

If you try to create instance of an abstract class, a compiler will would refuse to compile the program and returns an error message. For example, a declaration written as the following can produce an error due to the instantiated abstract class:

AbstractTest.java:6: class class_name is an abstract class……..

1 error

Abstract Methods

Methods that do not have implementation which are contained in abstract class are called Abstract methods. The abstract class can define an interface which can be used for programming using subclasses. The method declarations for all the necessary methods to implement the programming interface are stated on the subclasses.

In an object-oriented program, you may create circles, rectangles, Bezier and lines. These graphing objects have some similarities such as state and behavior. The state of the object can be defined through its position and bounding box. The behavior may be stated to move, resize or draw the object. These shared properties can be declared to inherit from a single parent object which can be defined as the class GraphicObject.

On the other hand, the objects are different depending upon what shape should be drawn.  The process of drawing rectangle is different from the one used to draw a circle. The following example clearly shows the properties of an abstract superclass.

The GraphicObject should be declared to create member variables and methods and to be shared with children classes. The superclass will have move_To(), current_position methods and are shared by all subclasses. There are other abstract methods that are declared by the abstract class GraphicObject but implemented in different ways. Such abstract methods is the draw() method, which has no default implementation but should be implemented in all subclasses. Putting it in syntax form, the class GraphicObject would appear as the following:

abstract class Example_of_Graphic_Object {

int x_var, y_var;

. . .

void move_To(int newX_var, int newY_var) {

. . .

}

abstract void draw_shape();

}

A non-abstract subclass of the class GraphicObject such as Rectangle_class and Circle_class should provide the implementation for the draw() method.

class Circle_class extends Example_of_Graphic_Object {

void draw_shape() {

. . .

}

}

class Rectangle_class extends Example_of_Graphic_Object {

void draw_shape() {

. . .

}

}

The abstract class may or may not contain an abstract method. However, any class containing an abstract method or does not have an implementation for the declared abstract methods in the superclasses should be treated as abstract class.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>