You are on page 1of 3

Abstract Class in java

What is Abstract Class in Java?

Java Abstract class is declared as abstract and can have both abstract and non-abstract
methods. It is a Java modifier applicable for classes and methods but not for variables. It
must be extended, and its method must be implemented.

Abstract classes in Java can’t be instantiated themselves but need to be sub-classed by other
classes to make the best of their properties. It is declared with the abstract keyword in its
class definition.

Important Things to Know About Java Abstract Class

A few notable points about abstract class in Java are:

 An abstract class and methods are declared using the abstract keyword.
 It has abstract and non-abstract methods.
 The abstract method doesn’t have an implementation (method body).
 Abstract class instances can’t be created.
 It supports constructors and static methods.
 Creating objects of abstract classes is not allowed.
 A class with abstract methods must also be abstract.
 An abstract class can’t be instantiated.
 You can create an abstract class without an abstract method.
 It can have a final method that doesn't allow he subclass to change the body of the
method. However, an abstract method in an abstract class can’t be declared as final. It
means that the final method can’t be abstract because it will show an error ‘Illegal
combination of modifiers: abstract and final.’
 You need to inherit subclasses and create their objects to implement features of the
Java abstract class.
 Use the abstract keyword to declare top-level classes and inner classes.
 In case there is one abstract method, then you must declare an abstract class.
 You need to override the subclass with abstract methods, but if a subclass is declared
abstract, overriding abstract methods is not necessary.
 Access static attributes and abstract class methods with the references of the abstract
class.
 If a child class fails to provide implementation to the abstract methods of the parent
class, it is essential to declare the child class as abstract. This will enable the next
child class to implement the rest of the abstract method.

Example 1: Shape
abstract class Shape {
protected int area;
public abstract void calculateArea();
public void displayArea() {
System.out.println("Area: " + area);
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public void calculateArea() {
area = (int) (Math.PI * radius * radius);
}
}
class Rectangle extends Shape {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public void calculateArea() {
area = length * width;
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5.0);
circle.calculateArea();
circle.displayArea();
Shape rectangle = new Rectangle(4, 6);
rectangle.calculateArea();
rectangle.displayArea();
}
}
Example 2: Animal
abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public abstract void sound();
public void displayInfo() {
System.out.println("Name: " + name);
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void sound() {
System.out.println("Meow");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void sound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Animal cat = new Cat("Whiskers");
cat.sound();
cat.displayInfo();
Animal dog = new Dog("Buddy");
dog.sound();
dog.displayInfo();
}
}

You might also like