Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Abstract Classes

Before understanding abstract classes it is necessary to understand abstract methods & concrete methods.

Concrete methods

Concrete means existing in reality & having complete meaning or structure.
Similarly, in Java, a well defined, method that is a method with the body is known as a concrete method.
This method has complete & proper meaning.

Syntax

accessModifier returnType methodName(parameterlist)
{
    //statements
}

Example

 public void concreteMethod()
{
    System.out.println("Concrete method");
}

Abstract method

Abstract means not existing in reality & not having complete meaning or structure.
It is just an idea that does not have physical existence.
Similarly, in Java, a method without a body is known as an abstract method.
This method does not have complete & proper meaning. It is just used to tell the purpose.

Syntax

accessModifier abstract returnType methodName(parameterlist);

Example

 public abstract void abstractMethod();

Abstract Class

A class that has abstract keyword at the beginning of its declaration is known as abstract class.
There is no compulsion for abstract class to have abstract method.
A class with abstract method must be declared as abstract.
We can’t create an object of the abstract class.

Syntax of an abstract class

accessModifier abstract class ClassName
{
    // Variables
    // One or more abstract methods
    // One or more concrete methods
}

Why abstract class needed in Java?

Abstract class is mainly used to create partially defined structure rather than fully defined structure.
There are some situations where we require constructing an abstract class, like suppose there are two or more classes with some common functions but may be executing in a different way. At that time it is better to have these classes a top-level class (superclass) with partially defined structure.
Top level class with partially defined structure is nothing but an abstract class.
The abstract class helps the programmer to design big software structures with ease, especially when many classes have some common functions and different way of execution.

How can we use the abstract class if it is not able to create objects?

To make use of an abstract class, we need to extend/inherit it by some class.

Syntax

accessModifier abstract class AbstractClassName
{
    // Variables
    // abstract methods
    // concrete methods
}

accessModifier class ClassName extends AbstractClassName
{
    // Variables
    // concrete methods
}

Note

The class that extends the abstract class should override the abstract methods of its superclass and provide an implementation for every abstract method.
Otherwise, the subclass which didn’t implement all the abstract methods of the superclass must be declared as an abstract class.

As we have stated that abstract class is usually used to create partially defined structure, it can be explained more easily by giving the real-life example as below:

Many times we see that top-level authorities leave some responsibilities to low-level authorities to implement the task.

Let’s consider a university at a top level and colleges affiliated to it at a low level.

Generally, University assigns a fixed syllabus for each and every field and their subjects. The same syllabus is taught by each and every college affiliated with that university.

But usually, a University does not assign a fix pattern/syllabus for sports activities that are held in every college at every year even though it is an important part of every academic year.

University leaves the responsibility on colleges to implement sports activities according to their requirement because every college might be interested in different sports.

Java Programming Language Abstract Class and its Implementation

Example of Java abstract class and its implementation by its subclasses

abstract class University 
{
    public void informationTechnology() 
    {
        System.out.println("Java Technologies");
    }
	
    public void electronics()
    {
        System.out.println("Analog Integrated Circuit Design");
    }
	
    public abstract void sports(); // not implemented. Leaved on colleges to
                                   // implement
}

class College1 extends University 
{
    @Override
    public void sports()
    {
        System.out.println("Basketball");
        System.out.println("Football");		
        System.out.println("Athletics");		
    }
}

class College2 extends University
{
    @Override
    public void sports()
    {
        System.out.println("Cricket");
        System.out.println("Badminton");
        System.out.println("Athletics");	
    }
}

class Abstractdemo
{
    public static void main(String args[])
    {
        College1 c1 = new College1();
        College2 c2 = new College2();
        System.out.println("College1 Information :: ");
        c1.informationTechnology();
        c1.electronics();
        c1.sports();
        System.out.println("\nCollege2 Information :: ");
        c2.informationTechnology();
        c2.electronics();
        c2.sports();
    }
}

Output

College1 Information ::
Java Technologies
Analog Integrated Circuit Design
Basketball
Football
Athletics

College2 Information ::
Java Technologies
Analog Integrated Circuit Design
Cricket
Badminton
Athletics

In the above program, we can create objects of class College1 & College2 but not of an abstract class University.

Abstract class without abstract method

We can create an abstract class without abstract method.
Sometimes it is necessary that some class should be prevented from creating an object & should be used only as a superclass for its subclasses. In such case class is declared as an abstract class.

Example

abstract class Test 
{
    public void concreteMethod()
    {
        System.out.println("This is concrete method");
    }
}

class WithoutAbstractMethod extends Test
{
    public static void main(String args[])
    {
        WithoutAbstractMethod ab = new WithoutAbstractMethod();
        ab.concreteMethod();
        //Test t = new Test();  
    }
}

Output

This is concrete method

If we remove the comment in the above program, the compiler will show error,
                                                       cannot instantiate the type Test
because class Test is declared as an abstract. This prevents Test class from creating the object.

Constructor in abstract class

Even though abstract class can’t create the object, it can contain instance variables that are necessary to create the object of its subclass.
To initialize these instance variables, the constructor in an abstract class is needed.

Example

abstract class Test2
{
    int x;
    int y;
	
    Test2(int a,int b)
    {
        x = a;
        y = b;
    }
}

class AbstractConstructor extends Test2
{
    int z;
	
    AbstractConstructor(int a, int b,int c)
    {
        super(a,b);
        z = c;
    }
 
    public void display()
    {
        System.out.println("x :: "+x);
        System.out.println("y :: "+y);
        System.out.println("z :: "+z);
    }
	
    public static void main(String args[])
    {
        AbstractConstructor ab = new AbstractConstructor(10, 20, 30);
        ab.display();		
    } 
}

Output

x :: 10
y :: 20
z :: 30

Java Programming Language Constructor in Abstract Class

Partial implementation of abstract class

Any subclass of an abstract class must implement all the abstract methods of the superclass (abstract class). If not, the subclass must be declared as abstract.

Example

abstract class University 
{
    public abstract void informationTechnology(); 
    
    public abstract void electronics();
    	
    public abstract void sports();
}

class College1 extends University
{
    @Override
    public void informationTechnology() 
    {
        System.out.println("Java Technologies");		
    }

    @Override
    public void electronics() 
    {
        System.out.println("Analog Integrated Circuit Design");
		
    }

    @Override
    public void sports() 
    {
        System.out.println("Basketball");		
        System.out.println("Athletics");		
    }
}

abstract class College2 extends University  // declared as an abstract
{
    @Override
    public void sports() 
    {
        System.out.println("Basketball");		
        System.out.println("Athletics");		
    }
}

In the above example, class College1 is not declared as abstract because it implements all the abstract methods of its superclass but class College2 is declared as abstract because it does not implement all the abstract methods of its superclass.

Abstract class reference to its subclass

Using abstract class reference, it is possible to refer to its subclass’ objects.
Using abstract class reference, it is possible to call correct version of a method based on the object which is assigned to abstract class reference.

Example

abstract class University 
{
    public abstract void sports();
}

class College1 extends University
{
    public void sports() 
    {
        System.out.println("Basketball");		
        System.out.println("Athletics");		
    }
}

class College2 extends University
{
    public void sports() 
    {
        System.out.println("Cricket");		
        System.out.println("Chess");		
    }
}

class AbstractClassRef 
{
    public static void main(String args[])
    {
        University u;
		
        u = new College1();
        u.sports();   // calls College1 sports method
        System.out.println("--------------");
        u = new College2();
        u.sports();   // calls College2 sports method
    }
}

Output

Basketball
Athletics
--------------
Cricket
Chess

Note

We can use the abstract class to create complete/fully abstract structure, but usually, an abstract class is used to create partially defined structure because it can contain variables & concrete methods also.
It is a bad programming to use the abstract class to create complete abstract structure. It may affect the performance of a program.
To create full abstract structure, Java has introduced one more concept named interface (explained later).

 

Leave a reply