Abstract Keyword in Java
Abstract keyword in Java is used to achieve abstraction. An abstraction is basically displaying only the details that are relevant to the end user. Using abstract keyword, the abstract classes and methods can be created.
The abstract classes are made abstract for data abstraction, so the only medium of extracting data from it is inheritance. It is not allowed to create objects of abstract classes. The abstract methods don't have any implementation, they are just declared so that other classes can override them and provide their implementation.
In Java, Abstraction is achieved using the abstract keyword. The abstract keyword is a non-access modifier. Abstract classes and methods can be created using the abstract keyword.
Rules Of Abstract Keyword
Before using the abstract keyword in your code, you must know some rules of the abstract keyword. There are some important do's and dont's for using abstract keyword.
- It is only possible to use the abstract keyword with classes and methods.
- It is mandatory that classes extending abstract classes must implement all the abstract methods of that parent class; otherwise it should also be declared as abstract.
- The inner class can be declared abstract by declaring it local.
- A static method and constructor can be part of an abstract class.
- When the final keyword is used, the abstract keyword cannot be used.
- Abstract methods cannot be declared private.
- Abstract methods cannot be declared static.
- An abstract keyword cannot be used with variables or constructors.
- An abstract class could not have an instance.
Abstract classes are restricted classes whose objects cannot be created. It is only possible to access the abstract class by inheriting the abstract class from some other class. But there must be a question in your mind. What's the use of a class if it's not possible to create its object? Let's take an example to understand it in a better way.
Why can’t we create an object of an abstract class?
An abstract class is largely undefined. It consists of abstract methods which are not implemented at all. An abstract class actually serves the purpose of defining the blueprints of classes that are going to inherit from it.
An abstract class has a protected constructor (by default) allowing only derived types to initialize it.
Also, let's say we are allowed to create objects of abstract classes. Now, what would happen when you call an abstract method using those objects? There would be no actual implementation of the method to invoke.
How to create an abstract class?
The syntax for creating an abstract class is given below :
In the above syntax abstract class is declared using the abstract keyword and the method inside it is also an abstract method which is declared using the abstract keyword.
Based on the example explained above let's create a Java program to understand the abstract class.
Please refer to the following article for a good understanding of Abstract Class - Click Here.
An abstract method is an undefined method, the type of method which does not contain any logic or definition but merely a declaration.
You cannot define the body of an abstract method at the time of its declaration. You must end all abstract methods with a semicolon whenever you declare them. You cannot declare abstract methods inside a regular (non-abstract) class, they are always present in abstract classes. Abstract methods serve as templates for abstract classes.
Let's take an example of College software. When you create an abstract class for College you need a few basic methods like admission, payroll, branch, etc. Thus, you simply write them as an abstract method inside the College class so that when the abstract class is extended you just have to define abstract methods.
How to create an abstract method?
The syntax for the abstract method is given below :
Let us see a Java program demonstrating the concepts we have learned so far.
- In the above program, inside a base abstract class Person we have declared display() method as abstract which has no body.
- After declaring the method as abstract in the base class we can override and define that method in any of the classes inheriting the base class.
- As we can see in the program the class Student extends the Person therefore the Student class is now eligible to override the abstract method display() and provide its implementation.
- The same goes for class Professor where we use abstract method display() to print professor data.
- Now, in the College class, we create the object of the classes which extends the base class, and on calling the abstract method, it will print the data according to the classes.
Examples of Abstract Keyword
Abstract class containing the constructor
- In the above program, we have an abstract class School inside which we have a constructor which initializes a String variable named alert.
- We also have an abstract method display() declared inside the abstract class.
- We have another class Student which extends the abstract class School, inside which we have a constructor for Student class which calls the super method with a string.
- The display() method is implemented in the child class Student which prints the value of alert variable.
- Now coming to the AbstractEg class we have made an object of Student class which extends abstract class School, we use that object to call the display() method printing Please pay the fees.
Abstract class containing overloaded abstract methods
- In the above program, we have an abstract class School inside which we have two overloaded abstract methods named display().
- In the first display(), the method does not contain parameters, whereas the second one takes one string parameter.
- Coming to the Student class, which inherits from abstract class School, here the two abstract methods are overridden.
- Now coming to the driver class OverloadedAbstMethods, we have created an object of Student class which first calls the abstract method, which prints Admission is made.
- Then while calling the second display method, we pass string value Alert! Please pay the fees, which get assigned to the string variable present in the second display() method, where we get both the strings as output.
Abstract And Final Keywords
Before going into the difference between abstract and final, let's briefly discuss final classes.
The term final class refers to a class that is declared using the Final keyword. Using the final keyword, you will finalize and close out the implementations of the methods, variables, and classes in this class.
After declaring a class as a final class, it is not possible to inherit from that class. If we extend it to another class, it will give us a compile-time error in Java. Also, we could not use final with abstract as an abstract class needs to be inherited to implement its methods, whereas final restricts inheritance.
Here is a small implementation of a final class:
If another class inherits from the final class, it will cause a compile-time error. The below code demonstrates this:
The above code will give you the following error:
Now, let's take a look at the difference between final class and abstract class.
|Abstract Class||Final Class|
|abstract keyword is used to declare an abstract class.||final keyword is used to declare a final class.|
|This helps to achieve abstraction.||This helps to restrict other classes from accessing its properties and methods.|
|Abstract classes cannot be instantiated.||It can be instantiated.|
|We can inherit an abstract class.||A final class cannot be inherited.|
|All abstract classes are meant to be overridden.||There is no concept of overriding in final classes as inheritance is not permitted.|
- Abstract keyword is used in Java to achieve Data Abstraction in OOP.
- It is only possible to use abstract keyword with classes and methods in Java.
- We cannot instantiate an abstract class (though it can have constructors) as it can contain abstract methods which do not have a body.
- There is no body in an abstract method. You must end all abstract methods with a semicolon whenever you declare them.
- Abstract methods cannot be declared private or static.
- Any class that extends an abstract class must implement all of its abstract methods; otherwise, that class needs to be declared abstract as well.
- Final classes cannot be inherited, and they cannot contain abstract methods.