Java Abstract Class Example

Abstract class in Java is a class which is created for abstracting the behaviour of classes from the outside environment. Abstract class can be created using 'abstract' keyword. An abstract class may contain abstract as well as non-abstract methods.

Java Abstract Class Example

Abstract class in Java is a class which is created for abstracting the behaviour of classes from the outside environment. Abstract class can be created using 'abstract' keyword. An abstract class may contain abstract as well as non-abstract methods.

Java Abstract Class Example

Java Abstract Class Example

In this section we will read about the Abstract class.

Abstract class in Java is a class which is created for abstracting the behaviour of classes from the outside environment. Abstract class can be created using 'abstract' keyword. An abstract class may contain abstract as well as non-abstract methods. It is not necessary that the abstract method must contained by the abstract class. Basic purpose to create an abstract class is for declaring a common behaviour which is like to implement in a superclass and some are unique to specific classes. An abstract class should not be declared as final because, if it will be declared as final then it can't be extended. Then the purpose of declaring an abstract class will not be complete. A subclass of an abstract class must have to define all of its abstract methods if the subclass doesn't do so then it must have also declared as abstract.

Declaration of abstract class

An abstract class should be declared as follows :

public abstract class AbstractClassDemo {
   // declare fields
   // declare non-abstract methods
   abstract void display();
}

Example

Here I am giving a simple example which will demonstrate you about how we can create an abstract class and how it can be used by its subclasses. We will see this example into two parts, in first part we will create an abstract class and we will try to create its object and call its method then we will try to compile and execute this program and then we will let see what happens after compiling and executing the program. In the second part we will create an another class by extending this abstract class then we will create an object of the subclass and call the method of abstract class. Then we will compile and execute the program and then we will let see what happens after compiling and executing the program.

Abstract Class

AbstractClassDemo.java

public abstract class AbstractClassDemo {
    private String name;

    public AbstractClassDemo(String name)
    {
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    abstract void display();

    public static void main(String args[])
      {
         AbstractClassDemo acd = new AbstractClassDemo("Roseindia");
         System.out.println("Absctract Class Example");
         acd.getName();
      }
}

When you will compile the above example then the compiler will give the error message as below :

But, when you will create a class by extending the abstract class and creates the object of subclass and abstract class as below :

AbstractClassDemo.java

public abstract class AbstractClassDemo {
private String name;

public AbstractClassDemo()
{
}

public AbstractClassDemo(String name)
{
this.name = name;
}

abstract void display();

public String getName(){
return this.name;
}
}

TestAbstractClassDemo.java

public class TestAbstractClassDemo extends AbstractClassDemo{

    void display()
     {
        System.out.println("display() of TestAbstractClassDemo");
     }
    public static void main(String [] args)
    {
        AbstractClassDemo acd = new AbstractClassDemo("Rose-India")
        {
              void display()
               {
                  System.out.println("display() of AbstractClassDemo");
               }
        };
        TestAbstractClassDemo tacd = new TestAbstractClassDemo() ;
        System.out.println(acd.getName());
        tacd.display();
         acd.display();
    }
}

When you will compile the above program then the output will be as follows :

An another way to create and call the methods of abstract class :

AbstractClass.java

abstract class AbstractClass {
 
  String name;  
  public void setName(String name)
   {
      this.name = name;
   }
   public String getName()
   {
      return this.name;
   }
   abstract void display(); 
}

FirstClass.java

public class FirstClass extends AbstractClass
 {
     void display()
      {
          System.out.println("display() method implemented in FirstClass");
      }
 }

SecondClass.java

public class SecondClass extends AbstractClass
 {
     void display()
      {
          System.out.println("display() method implemented in SecondClass");
      }
 }

TestAbstractClass.java

class TestAbstractClass
 {
     public static void main(String args[])
       {
         
          FirstClass first = new FirstClass();
          SecondClass second = new SecondClass();
          System.out.println("\n*************Output*************");
          System.out.println("Abstract Class Example");
          first.setName("First Name : Rose");
          second.setName("Second Name : India");
          
          first.display();
          System.out.println(first.getName());
          second.display();
          System.out.println(second.getName());  
          System.out.println("\n*************END****************");
       }
 }

Output :

When you will compile and execute the TestAbstractClass.java class then the output will be as follows :

Download Source Code