codescracker


java

Java Abstraction



« Previous Tutorial Next Tutorial »


In Java, Abstraction is the ability to make a class abstract in Object Oriented Programming (OOP).

Java abstract Class

An abstract class is the one that cannot be instantiated. All the other functionality of the class still exists. Its fields, methods, and constructors are all accessed in the same manner. You cannot create an instance of the abstract class.

If a class is abstract and can't be instantiated, the class does not have much use, it is subclass. This is typically how the abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but parent class itself is too abstract to be used on its own.

There are some situations in which you will want to define a superclass that declares the structure of a given abstraction without providing the full implementation of every method i.e., sometimes you will want to create a superclass that only defines a generalized form which will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such class determines the nature of the methods that the subclasses must implement.

One way this situation can take place is when a superclass is unable to create a meaningful implementation for a method. When you create your own class libraries, it is common for a method to have no meaningful definition in the context of its superclass.

You can handle this situation in two ways. One way, is to simply have it report a warning message. While this approach can be useful in certain (definite but not specified) situations, such as debugging, it is not usually appropriate. You may have methods that must be overridden by the subclass in order for subclass to have any meaning. Consider the class Triangle. It has no meaning if the area() is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all the necessary methods. Java's solution to this problem is the abstract method.

You can require that certain methods to be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as a subclasser responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them, it can't simply use the version defined in the superclass.

Declare an abstract Method

To declare an abstract method, use the following general form :

abstract type name(parameter-list);

As you can see, no method body is present.

A class, contains one or more abstract methods must also be declared as abstract.

Declare an abstract Class

To declare a class abstract, you simply use the abstract keyword in from of class keyword at the beginning of class declaration as shown here.

abstract class class_name

An abstract class can't be directly instantiated with operator new. Such objects will be useless, because an abstract class is not entirely defined. Also, you can't declare abstract constructors, or abstract static methods. Any subclass of an abstract class must implement all of the abstract static methods.

Java abstract Class and abstract Method Example

Following is a simple example program of a class with an abstract method, followed by a class which implements that method :

/* Java Program Example - Java Abstraction Example
 * This program demonstrates abstract class */
 
 
 abstract class A
 {
     abstract void callme();
     
     /* the concrete methods are still allowed in the
      * abstract classes      */
      void callmealso()
      {
          System.out.println("This is a concrete method");
      }
 }
 
 class B extends A
 {
     void callme()
     {
         System.out.println("B's implementation of callme");
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
         
         B obj = new B();
         
         obj.callme();
         obj.callmealso();
         
     }
 }

When the above Java program is compile and executed, it will produce the following output:

java abstraction

Notice that no objects of the class A are declared in the above program. As mentioned that it is not possible to instantiate an abstract class. Also class A implements a concrete method called callmealso(). This is perfectly acceptable. Abstract classes can include as much implementation as then see fit.

Even though, abstract classes can't be used to instantiate objects, they can be used to create object references, because approach of Java to run-time polymorphism is implemented through the use of the superclass references. Thus, it must be possible to create a reference to the abstract class so that it can be used to point to a subclass object. You will see this feature put to use in the upcoming example program.

The following version of the program declares area() is abstract inside the Figure. This means that all the classes derived from the Figure must override the area().

/* Java Program Example - Java Abstraction Example
 * This program uses abstract methods and
 * classes */
 
 abstract class Figure
 {
     double d1;
     double d2;
     
     Figure(double x, double y)
     {
         d1 = x;
         d2 = y;
     }
     
     /* area is now an abstract method */
     abstract double area();
 }
 
 class Rectangle extends Figure
 {
     Rectangle(double x, double y)
     {
         super(x, y);
     }
     
     /* override area for rectangle */
     double area()
     {
         System.out.println("Inside Area for Rectangle : ");
         return d1 * d2;
     }
 }
 
 class Triangle extends Figure
 {
     Triangle(double x, double y)
     {
         super(x, y);
     }
     
     /* override area for right triangle */
     double area()
     {
         System.out.println("Inside Area for Triangle : ");
         return d1 * d2 / 2;
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
         
         /* Figure fig = new Figure(100, 100);      // illegal now!! */
         
         Rectangle rec = new Rectangle(90, 50);
         Triangle tri = new Triangle(100, 80);
         Figure figref;              // this is OK, no object is created
         
         figref = rec;
         System.out.println("Area is " + figref.area());
         
         figref = tri;
         System.out.println("Area is " + figref.area());
         
     }
 }

When the above Java program is compile and executed, it will produce the following output:

java abstract class methods

As the comment inside the main() indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all the subclasses of the Figure must override the area(). To provide this to yourself, try creating a subclass that does not override the method area(). You will receive a compile-time error.

Even though it is not possible to create an object of type Figure, you can create a reference variable of type Figure. The variable figref is declared as a address to the Figure, which means that it can be used to refer to an object of any class derived from the Figure. As explained, it is through superclass reference variables that overridden methods are resolved at run time.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test