Abstract Classes in Java


There are situations where you would like to define a superclass that declares the structure of a given abstraction without fully applying each method. That is, sometimes you want to create a superclass that simply defines a generalization form that will be divided by all its subclasses, leaving it in each subclass to fill in the details. This class determines the nature of the subclass methods to be applied. One way this situation can occur is when a superclass is unable to create a meaningful implementation for any method.


Specific methods need to be overridden by subclass specifying abstract type modifiers. These methods are sometimes referred to as subclass responsibilities because they have no specification in the superclass. So, a subclass must override them - it can't just use the version defined in the superclass. To declare an abstract method, use this simple form:


abstract type name(parameter-list);

Any class that has one or more abstract methods must be declared abstract. To declare an abstract class, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. Abstract class objects cannot exist. That is, no abstract class can be instantaneously connected to a new operator. Such objects will be useless because an abstract class is not fully defined. Also, you cannot declare abstract constructor or abstract static method. Any subclass in the abstract class must either apply all abstract methods to the superclass or declare itself abstract. Here is a simple example of a class with an abstract method, followed by a class that applies that method:

Example of abstract class and methods

Program
// A Simple demonstration of abstract.
abstract class X
{
 abstract void Show();
 
 // concrete methods are still allowed in abstract classes
 void CallMe()
 {
  System.out.println("This is a concrete methods.");
 }
}
class Y extends X
{
 void Show()
 {
  System.out.println("Y implementation of CallMe.");
 }
}
public class Main 
{
  public static void main(String[] args) 
  {
    Y y1=new Y();
    
    y1.Show();
    y1.CallMe();
  }
}

Notice that no objects of class "X" are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class "X" implements a concrete method called "CallMe()". This is perfectly acceptable.


Example

Although object classes cannot be used to instantiate objects, they can be used to generate object references, as Java uses superclass references toward run-time polymorphism. Thus, it would certainly be possible to create a reference for an abstract class so that it could be used to point to a subclass object.

Program
// Using abstract methods and classes.
abstract class Example
{
 double first,second;
 Example(double f,double s)
 {
  this.first=f;
  this.second=s;
 }
 // Area() is now an abstract method.
 abstract void Area();
}
class Rectangle extends Example
{
 Rectangle(double a,double b)
 {
  super(a,b);
 }
 // Override Area() for rectangle
 void Area()
 {
  double a=first*second;
  System.out.println("Area of Rectanagle : "+a);
 }
}
class Triangle extends Example
{
 Triangle(double c, double d)
 {
  super(c,d);
 }
 // Override Area() for triangle
 void Area()
 {
  double a=(first*second)/2;
  System.out.println("Area of Triangle : "+a);
 }
}
public class Main 
{
  public static void main(String[] args) 
  {
    //Example e1=new Example(4,4); //Error
    Rectangle r1=new Rectangle(5,5);
    r1.Area();
    
    Triangle t1=new Triangle(4,6);
    t1.Area();
  }
}