by Dinesh Thakur Category: Inheritance

Some time there is a situation in which you feel the need of a superclass that has only declaration of few or all methods, with definition of few or none methods. But it is necessary that it must not be a completely defined or implemented class. The methods are declared only and specified by abstract type modifier called abstract method.

An abstract method is a method prototype (i.e. return type, method name, list of parameters and optionally throws clause) without any implementation. Its implementation is provided by the subclass(es) of the class in which it is declared. To create an abstract method, simply specify the modifier abstract followed by the method declaration and replace the method body by a semicolon. To declare any method as abstract use the following form:

abstract accessmodifer returntype methodName(<parameterlist>);

A class can have one or more abstract methods, that class must also be declared as abstract. To declare a class as abstract, simply add abstract keyword before the class keyword in the first line of class definition. Abstract class cannot be instantiated, i.e. you cannot create an object with the new operator of abstract class. The construct cannot be abstract, and no method can be abstract static. Any concrete subclass has to implements all the abstract method of its abstract superclass. Abstract classes can be used to create object references. For example: The Shape superclass that contains abstract method area () will now look like,

abstractclass Shape {

      abstractdouble area();//abstract method

      abstractdouble circumference();

}

Abstract classes are like normal classes with fields and methods but you cannot create objects of abstract classes using the new operater. However, you can declare a variable of an abstract class type. Such variables are used to manipulate subclass objects polymorphically. In other words, such variable can be used to refer to an instance of any of the subclasses of abstract superclass.
For example: If you try to instantiate an object of the abstract superclass Shape using the following statement,

Shape s = new Shape(3,4);// error, Shape abstract

It will result in compile time error. Moreover, it makes no sense to create Shape object. What dimensions would it have? What would be its area? .
Abstract classes are useful when you want to create a generic type that is used as a superclass for two or more subclasses, but the superclass itself does not represent an actual object. For example: As in case of Shape class which we need for inheritance and polymorphism but want only to instantiate objects of its subclasses, not Shape itself.

The following is java abstract class example.

//Show how to create abstract class and method

abstract class Shape {

      abstract void area();

      abstract void circumference();

}

class Rectangle extends Shape {

       private double length ,breadth;

       Rectangle(double x,double y) {

               length = x; breadth = y

       }

           public void area() {

                 System.out.println("Area of Rectangle is = " + (length*breadth));

                 public void circumference() {

                       System. out.println ("Circumference of Rectangle is=" +2* (lengthtbreadth));

                 }

           }

class Circle extends Shape {

          private double radius; Circle(double r) {

                 radius = r ;

          }

          public void area() {

              System.out.println("Area of Circle is= "+( Math.PI*radius*radius));

          }

          public void circumference() {

              System.out.println("Circumference of Circle is="+ 2*Math.PI*radius);

          }

}

      class AbstractDemo {

             public static void main(String[] args) {

                 Shape s; //Shape class reference variable

                 Rectangle r = new Rectangle(10,20);

                 s = r; // Assign rectangle reference to shape reference

                 s.area();

                 s.circumference();

                 Circle c = new Circle(5);

                 s = c; //Assign circle reference to shape reference

                 s.area();

                 s.circumference();

             }

       }

The following points should be kept in mind while working with abstract classes.

• A subclass of an abstract class can be instantiated only if it overrides each of the abstract methods of its superclass and provides an implementation for each of them. Such classes are known as concrete classes (i.e. not abstract).
• If a subclass does not implements all the abstract methods that it inherits, the subclass must be specified as abstract.
• An abstract method cannot be private. Since a private method cannot be inherited and therefore cannot be overridden (redefined) in the subclass.
• Constructors and static methods cannot be declared abstract. As constructors cannot be inherited so an abstract constructor could never be implemented. Also as subclasses cannot override static methods, so an abstract static method cannot be implemented.
• An abstract class has no use, no purpose unless it is extended. However, abstract superclass names can be used to invoke the static methods declared in those abstract superclasses.
• By marking the class abstract, the compiler will stop any code, anywhere, from ever creating an instance of that type.
• Not all the methods in an abstract class have to be abstract.
• You can declare the class as abstract even if it does not have any abstract method. Such abstract class indicates that the implementation is incomplete and is meant to serve as a superclass for one or more subclasses that will complete the implementation.
• Class can't specify both abstract and final. As the abstract class can only be used if you subclass it and final class cannot be subclassed.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.