Abstract Class in Java

Abstract Classes are used when a parent class needs to provide default behavior along with abstract methods. It enforces child classes to implement specialized behavior of abstract methods. It can never be Instantiated.

An abstract class is declared by abstract keywords.

public abstract class Shape{
	// add some text here
}

An abstract class has the following properties:

1. It may or may not contain abstract methods.

2. It can be initiated.

3. Child classes are enforced to implemented abstract methods.

4. If child class does not implement abstract methods then the child will also be declared as abstract.

If a class is not an abstract class then it is called a concrete class. The concrete class does not have abstract methods.

abstract class Demo {
  public abstract void animalSound();
  public void sleep() {
    System.out.println("Demo");
  }
}

From the example above, it is not possible to create an object of the Demo class:

Demo obj = new Demo(); // will generate an error

Share