In this tutorial, we will learn about Java abstract classes and methods and how to use them in our program with the help of examples.
Abstract Class
An abstract class is a class that cannot be instantiated (we cannot create objects of an abstract class).
Syntax
We use the abstract keyword to declare an abstract class.
abstract class class_name { //fields and methods }
If we try to create objects of an abstract class, we will get a compilation error. For example,
//abstract class abstract class Person { //class members private String name; public void setName(String name) { this.name = name; } } class Main { public static void main(String args[]) { //creating Person instance //error: 'Person' is abstract; cannot be instantiated Person person = new Person(); } }
We can access the members of a class by creating its object or by creating an object of its subclass.
Abstract classes cannot be instantiated but we can create subclasses from it. We can create objects of subclasses to access members of the abstract class.
Abstract class is used to define common characteristics of subclasses and the only purpose of the abstract class is to let other sub classes inherit from it.
Before we learn about it in detail, we need to understand abstract methods.
Abstract Method
Abstract class can contain methods that contain no implementation (without any body) called abstract method.
Syntax
We use the same keyword abstract to create abstract methods.
abstract return_type function_name(); //No Implementation
An abstract class contains fields and methods similar to other classes and addition to this, it can also contain abstract methods.
If a class has any abstract method then the entire class must be declared as abstract.
When to use abstract methods?
Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.
Why can’t we create the object of an abstract class?
Abstract classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then there would be no actual implementation of the method to invoke.
Overriding abstract methods
An abstract class cannot be instantiated; therefore we must inherit it so that we can access the members of an abstract class.
When an abstract class is subclassed,
- To create an object of subclass, we must have to override (provide implementation to a method already defined in superclass) all the abstract methods of super class in our subclass.
- However, if subclass does not implement abstract method, then the subclass must also be declared abstract (which cannot be instantiated).
Example
//abstract class Person abstract class Person { String name; //abstract method abstract void show(); //non-abstract method public void setName(String name) { this.name = name; } } //subclass Student inherit Person class class Student extends Person { private int rollNo; public void setRollNo(int rollNo) { this.rollNo = rollNo; } //Implementing the method of Person class in Student class public void show() { System.out.println("Student Detail"); System.out.println(" Name: " + name + " Roll no: " + rollNo); } } //subclass Teacher inherit Person class class Teacher extends Person { private int salary; public void setSalary(int salary) { this.salary = salary; } //Implementing the method of Person class in Teacher class public void show() { System.out.println("Teacher Detail"); System.out.println(" Name: " + name + " Salary: " + salary); } } class Main { public static void main(String[] args) { //creating Student class instance Student s1 = new Student(); s1.setName("Arun Verma"); s1.setRollNo(101); s1.show(); //creating Teacher class instance Teacher t1 = new Teacher(); t1.setName("Ramesh Sahu"); t1.setSalary(25000); t1.show(); } }
Output
Student Detail Name: Arun Verma Roll no: 101 Teacher Detail Name: Ramesh Sahu Salary: 25000
Example explained
- Here, we have created an abstract class Person. This class contains an abstract method show(), a non-abstract method setName() and a field name.
- We have inherited subclasses Student and Teacher from the super class Person. The subclasses (Student and Teacher) overrides the abstract method show().
- Now creating an instance of subclasses we cannot only access its members but we can also access its superclass (abstract class Person) members.
Creating abstract class reference variable
We cannot create instance of abstract class but we can create reference variable of abstract class.
Example
abstract class Base { public void funA() { System.out.println("Base funA() called"); } //abstract method abstract void funC(); } class Derived extends Base { public void funB() { System.out.println("Derived funB() called"); } //Implementing funC() of Base class in Derived class void funC() { System.out.println("Derived funC() called"); } } class Main { public static void main(String args[]) { //error:'Base' is abstract; cannot be instantiated //Base b = new Base(); // We can have references of Base type. Base b = new Derived(); b.funA(); b.funC(); //error: using base class reference variable we cannot access derived class members //b.funB(); } }
Output
Base funA() called Derived funC() called
Note: Using Base class reference variable we cannot access derived class members.
Abstract class constructor
An abstract class can contain constructors and it is called when an instance of inherited class is created.
Example
// An abstract class with constructor abstract class Base { //Base class default constructor Base() { System.out.println("Base Constructor Called"); } //abstract method abstract void fun(); } class Derived extends Base { //Derived class default constructor Derived() { System.out.println("Derived Constructor Called"); } void fun() { System.out.println("Derived fun() called"); } } class Main { public static void main(String args[]) { //creating Derived class instance Derived d = new Derived(); } }
Output
Base Constructor Called Derived Constructor Called
Visit Java Constructor in Inheritance to learn more about Constructor behavior in Inheritance.
Points to Remember:
- To create abstract classes and methods we use the abstract keyword.
- An abstract method doesn’t have any implementation (method body).
- A class containing abstract method must be declared as abstract.
- An abstract class can contain abstract and non-abstract methods.
- Abstract classes cannot be instantiated but we can create subclasses from it.
- Abstract class can have constructors and final methods (which will force the subclass not to change the body of the method).
-
An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass.staticMethod())
- The class inheriting the abstract class must either be declared abstract or implement abstract method.
- To create an object of a class that extends abstract class we must have to implement its abstract methods in our class.
- If the class inheriting the abstract class is declared abstract, it’s not mandatory to override abstract methods but now we cannot create an instance of subclass.