Abstract Class and Abstract Methods in Java

In the previous tutorial, we learned about the Inheritance in Java with example programs. In this tutorial, we will learn about Abstract Class and Abstract Methods in Java.

232

What is Abstract Class in Java-

Unlike C/C++, Java provides an abstract keyword for creating the abstract class.

An abstract class is a class that cannot be instantiated.

Why abstract class in Java?

Many times, we need to define some functions in a class, so that those functions can be used by creating a sub-class of that object, but, creating an object of the class will be of no use of not required. In this case, we use the abstract keyword to create the class as abstract

For example, there is a school with faculty and students. And there are some similar methods and variables between student and faculty such as name, age, phone number, etc. To avoid defining these methods twice, we will create a class for example ‘person’ class and we will write the common functions in this. Then, we will create the class student and faculty which is sub-class of this person class. But, it is possible that any programmer makes the object of the Person class too. This will be of no use. To avoid this, we use the abstract keyword which will limit the user from creating its sub-class.

Features of Abstract Class in Java–

  • In Java, abstract classes are used to define some common characteristics of its sub-classes
  • It can only be used as a superclass for its sub-classes
  • Like other classes, it contains fields that describe characteristics and methods that describe actions that a class can perform.

Example –

abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run() {System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}

So, in object-oriented programming, sometimes it becomes necessary to make abstract classes to save time in writing some functions again and again.

Now, we will learn about abstract methods in Java.

Abstract Methods in Java

An abstract class can contain methods that contain no implementation. These are called abstract methods. The abstract method ends with a semi-colon rather than with a block.

It is to be noted, that an abstract class may or may not have an abstract method. But, we cannot create an abstract method without an abstract Class. We will understand this concept better with the help of the example given below –

class Person{
abstract void show(); }
class Student extends Person{}
class abstractexample{
public static void main(String[] args){
Student s = new Student();}
}

This piece of code will result in an error, because, we have created an abstract function but the class containing the abstract function is not abstract which is not possible in Java. We can add abstract keywords before the class person. But will this resolve all the errors?

No, we will still have an error remaining. Because, the class student extends the person class, so the abstract method in the parent class will also exist in the child class. Therefore, we will need to add the abstract keyword before the Student class too.

The correct code is written below –

abstract class Person{
abstract void show(); }
abstract class Student extends Person{}
class abstractexample{
public static void main(String[] args){
Student s = new Student();}
}

But still we do not know the reason behind creating this abstract class.

Why abstract Methods?

We make a class as abstract to force the programmer to create the sub-class of the class containing the abstract method to create that function.

For example, we have a class Account and inside it, there is a function called ‘rate of interest’. If any user created the sub-class of this class, we want it mandatory to create the ‘rate of interest’ function inside that. If we don’t override the rate of interest function inside the sub-class, the same abstracted method of the parent class will be passed, and for that, we will again need to declare the sub-class as abstract. And we further cannot instantiate even the sub-class.

So, the abstract method is very useful to force the programmer to write a specific function in his class.

Quiz on Java:

Previous QuizInheritance in Java with example programs
Next QuizInterface In Java

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.