---
title: Abstract Class in Java - Scaler Topics
description: Learn about abstract class in Java by Scaler Topics. This article will help you understand all the concepts of Abstract Classes in Java. Read to learn more.
author: Shikhar Baniya
category: Java
---
:::section{.abstract}
You might wonder, "What is an abstract class in Java?" An abstract class cannot be instantiated and is primarily meant to be subclassed by other classes.
A class that is declared with the keyword having `abstract` is known as an abstract class in Java. It may have abstract and non-abstract methods. An abstract method is an method that is declared without any implementation. Abstract classes in Java implement the concept of [Abstraction](https://www.scaler.com/topics/java/abstraction-in-java/) in Object Oriented Programming.
:::
:::section{.main}
## What is Abstract Class in Java?
An abstract is a small description that gives you an imaginative idea of the concept. Like a summary, a small paragraph gives you a brief explanation of the whole book.
In the same way, **Abstract Classes** in Java simply declares the methods that need to be implemented by the class, which extends the abstract class. The abstract class hides the internal details of method implementation, showing only essential information to the user, such as the method name.
*Hiding internal details and showing only the functionality is known as abstraction.* Abstraction is one of the key concepts in the object-oriented programming paradigm.
**To implement Abstraction, we use Abstract Classes or Interfaces**. Abstract class in Java is a collection of abstract and non-abstract methods. Abstract methods do not have a body or implementation. The child classes, which extend the abstract class, provide the implementation of the abstract methods.
An Abstract Class is nothing but a blueprint for the child class. *Abstract classes justify the layout of your idea and do not really implement them.*
**Syntax:**
```java
abstract class ClassName {
// Abstract and Non-abstract methods
}
```
### Illustration
```java
abstract class Circle
{
int height;
// An abstract function
abstract void area();
}
```
## Some Important Points about Abstract Classes
Let's recap the rules to declare an abstract class -
* You have to use the keyword `abstract`.
* You cannot instantiate an abstract class.
* An abstract class can contain both abstract and non-abstract methods.
* You can include non-abstract final methods (a method that cannot be overridden) in your abstract class as well.
* Final methods in abstract classes can not be abstract. They must be implemented in the abstract class itself.
* You can also include constructors and non-abstract static methods in your abstract class.
:::
:::section{.tip}
**Must Read- [When do We Use an Abstract Class in Java?](https://www.scaler.com/topics/when-do-we-use-an-abstract-class-in-java/)**
:::
:::section{.main}
## Abstract Method in Java
Using the **abstract** keyword while declaring the method is called an Abstract Method. An [Abstract Method](https://www.scaler.com/topics/abstract-method-in-java/) does not contain anybody. Whenever you declare an abstract method, you must end it with a semicolon, as shown in the syntax below. An abstract method is always present inside an abstract class; you cannot declare it inside the regular (not abstract) class.
Let's understand some of the rules on the usage of the Abstract Method -
* A method declared with the abstract keyword is called an Abstract method.
* Abstract method can only be declared inside an abstract class or an interface.
* Abstract methods must not contain any definition or body in abstract class.
* You must end the declaration of the abstract method using ';'(semicolon).
* To write the implementation code of these abstract methods, you must inherit the abstract class. Then, the child class can define the abstract methods.
* If you don't define the abstract method inside the child class, then you must declare the child class as abstract; otherwise, the compiler will throw an error. This is illustrated below:
```java
abstract class Calc {
abstract void display();
}
class Computer extends Calc{
}
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```
Output:
```java
Main.java:17: error: A is not abstract and does not override abstract method display() in Calc
class A extends Calc{
^
1 error
```
### Syntax of Abstract Method
The syntax for the abstract method is similar to any user-defined method in Java, except you have to add the **abstract** keyword at the start of the declaration. For example -
```java
abstract return_type method_name(parameters list);
```
### Java Abstract Class Examples
Let's understand abstract class and methods in depth using a Java example.
### Example of Abstract Class That Has Abstract Method
**See the following example -**
```java
abstract class Calculator
{
abstract void display();
}
class Add extends Calculator
{
void display()
{
System.out.println("In the Add Child Class");
}
}
class Sub extends Calculator
{
void display()
{
System.out.println("In the Sub Child Class");
}
}
class AbstractMethodExample
{
public static void main(String arg[])
{
Calculator add = new Add();
add.display();
Calculator sub = new Sub();
sub.display();
}
}
```
**Output:**
```java
In the Add Child Class
In the Sub Child Class
```
**Explanation:**
* We have created two classes - `Add` and `Sub` which extend the parent abstract class `Calculator`.
* Since `Add` and `Sub` inherit the abstract class, they must implement the abstract method `display()`; otherwise, an error will be thrown.
* Next, we declare a class **'Main'**. In the main method, using the `Calculator` abstract class, we declare two objects, `add` and `sub`. Now, using these objects, we can access the methods inside the child class and hence the output.
### Abstract Class Having Constructor, Data Member, and Methods
```java
import java.io.*;
abstract class Animal {
String name;
Animal(String name) {
this.name = name;
System.out.println("A new " + name + " is created");
}
abstract void sound();
void eat() {
System.out.println(name + " is eating");
}
}
class Dog extends Animal {
Dog() {
super("Dog");
}
void sound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
Cat() {
super("Cat");
}
void sound() {
System.out.println("Meow");
}
}
class Main {
public static void main(String[] args) {
Animal pet1 = new Dog();
Animal pet2 = new Cat();
pet1.sound();
pet1.eat();
pet2.sound();
pet2.eat();
}
}
```
**Output:**
```
A new Dog is created
A new Cat is created
Woof
Dog is eating
Meow
Cat is eating
```
**Explanation:**
* When a new Dog() is called, it invokes the constructor of the Dog class, which calls the constructor of the Animal class with the name "Dog". This prints "A new Dog is created".
* Similarly, when a new Cat() is called, it invokes the constructor of the Cat class, which in turn calls the constructor of the Animal class with the name "Cat". This prints "A new Cat is created".
* pet1.sound() invokes the sound() method of the Dog class, printing "Woof".
* pet1.eat() invokes the eat() method of the Animal class with the name "Dog", printing "Dog is eating".
* pet2.sound() invokes the sound() method of the Cat class, printing "Meow".
* pet2.eat() invokes the eat() method of the Animal class with the name "Cat", printing "Cat is eating".
:::
:::section{.main}
## Properties of Java Abstract Class
* Use the **'abstract'** keyword to declare a class or method abstract.
* You have to implement all the abstract methods declared within the abstract class in its subclasses.
* The class has to be abstract if you want to declare an abstract method within its scope.
* You cannot create objects of an abstract class because the abstract class cannot be instantiated.
* Whenever you create an object of the child class of an abstract class, the compiler calls the constructor of the abstract class automatically.
* You can declare non-abstract final and static methods inside abstract class in java.
* Multiple inheritances cannot be achieved using an abstract class.
:::
:::section{.summary}
## Conclusion
* Abstraction is one of the key principles for implementing the Object-Oriented Programming Approach and design in Java.
* When you use the ‘abstract’ keyword while declaring a method, it is called an abstract method.
* Abstract methods are implemented in the subclasses of the abstract class within which they are declared.
* Abstract class in Java can partially implement one or more interfaces.
* We can declare non-abstract static and final methods in Java. We must provide their implementation in the abstract class itself.
* We cannot create instances of abstract classes because they may contain non-implemented methods that have no operations to perform.
:::
:::