Home  >  Articles  >  Abstract Class in Java

Abstract Class in Java

Nupur yadav

Updated on 20th June, 2024 , 3 min read

Introduction

 

Java is a programming language that is high level, based on class and object-oriented which is made in such a way that very few dependencies. It is a very general-purpose language and is also WORA, i.e. write once run everywhere. It essentially means that you can run the compiled code on all platforms without further compilation required.

Now in the Java programming language understanding Abstract classes is imperative to create a base for other classes to build upon. In this article, we will read about what is an abstract class, explain abstract class, abstract class in Java, and abstraction in Java with example

abstract class meaning, java abstract class example, abstract class and method in java, what is the purpose of abstract class and php abstract class.

 

What is Abstract Class?

 

It is a class that can not be started independently. It is made to be a superclass that gives a common platform for its sub-classes and also potentially contains both abstract and non-abstract methods. The thing about the abstract method is that it can be implemented with the subclasses only and is often declared without implementation. Now that we understand the abstract class, let’s understand the key characteristics of the abstract class.

abstract class Animal {
    abstract void makeSound();
    void breathe() {
        System.out.println("This animal is breathing.");
    }
}

What are the characteristics of Abstract Classes?

 

There are around 4 major characteristics of abstract classes, these are listed below:

  • Can’t be instantiated directly: as we read above they need a subclass to be instantiated. 
  • Mix Methods: they contain a mix of methods abstracts and concrete methods. 
  • Constructors: when the subclasses are instantiated the constructors are called. 
  • Inheritance: abstract classes have the ability to inherit from other classes but a class can only inherit from one abstract class.

Advantages of Using Abstract Class: Abstract classes are used when there are numerous related classes but they have one common structure but are different in some specific behaviours.

 

Examples of Abstract Class

 

Example 1: Abstract Shape Class

Assuming you know a few basics let's understand a case where we have a graphical application where some shapes like circles, and rectangles, need to be drawn. Each shape has common properties like colour and areas but is different in the sense of how they are being drawn:

abstract class Shape {
    String color;
    
    Shape(String color) {
        this.color = color;
    }
    
    abstract void draw();
    
    abstract double area();
    
    void displayColor() {
        System.out.println("Color: " + color);
    }
}

class Circle extends Shape {
    double radius;
    
    Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }
    
    @Override
    void draw() {
        System.out.println("Drawing a Circle.");
    }
    
    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    double length, breadth;
    
    Rectangle(String color, double length, double breadth) {
        super(color);
        this.length = length;
        this.breadth = breadth;
    }
    
    @Override
    void draw() {
        System.out.println("Drawing a Rectangle.");
    }
    
    @Override
    double area() {
        return length * breadth;
    }
}

Example 2: Abstract Animal Class
 

Let's understand the classic animal example in a zoo different animals exhibit different characteristics but have common traits of eating and sleeping. 

abstract class Animal {
    String name;
    
    Animal(String name) {
        this.name = name;
    }
    
    abstract void makeSound();
    
    void eat() {
        System.out.println(name + " is eating.");
    }
    
    void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

class Lion extends Animal {
    Lion(String name) {
        super(name);
    }
    
    @Override
    void makeSound() {
        System.out.println("Roar!");
    }
}

class Elephant extends Animal {
    Elephant(String name) {
        super(name);
    }
    
    @Override
    void makeSound() {
        System.out.println("Trumpet!");
    }
}

Abstract Vs Interface

While both of them are used to achieve abstraction they have different goals, let's understand them below:

Interface:

Cannot have an instance variable (before the advent of Java 8 interfaces could not have default modes. 
Used to define the contract of methods without any implementation. 
One class can facilitate numerous interfaces, which means more flexibility.

Abstract class: 

Can have instance variables and constructors
Chave both abstract and concrete methods
The subclasses have a common base class and hence have shared behavior and state.

Conclusion:

Abstract classes in Java are a potent feature that allows for the creation of a common base for the related classes that have shared functions and method implementation is done through abstract methods. By using this method we can ensure a well-structured and manageable codebase, which makes making complex applications easy. So if you are using a graphical application of managing a zoo abstract classes come in handy and can help in enhancing your Java programming skills. 

Frequently Asked Questions

What is an abstract class in Java?

Abstract class is one that can’t be used to create objects, in order to use it one has to take it from the inherited class.

Why use abstract class?

Abstract classes are found useful when one needs to explain hierarchies of classes that have a common attribute.

What do you mean by abstraction in Java?

Abstraction in Java helps in the efficient development of software. It helps programmers build simplified models of the real life systems. It allows them to concentrate on the essential features and hide the implementation details.

What is the difference between an abstract class and an interface in Java?

Abstract classes can have both concrete and abstract methods and can hold state, while interfaces define a contract with abstract methods (and optionally default methods) that implementing classes must fulfill. A class can extend one abstract class but implement multiple interfaces.

Check Eligibility Apply Now