Abstract Keyword in Java
In Java, the abstract keyword facilitates abstraction, which focuses on displaying only relevant details. Abstract classes and methods created using this keyword cannot instantiate objects directly and require inheritance for data extraction. Abstract methods, lacking implementation, are designed for overriding in subclasses. This approach simplifies complex systems by highlighting essential features.
Rules Of Abstract Keyword
Before using the abstract keyword in your code, you must know some rules. There are some important do's and do n'ts for using abstract keywords.
Do's :
- Using the abstract keyword with classes and methods is only possible.
- 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.
Dont's :
- The abstract keyword cannot be used when the' final' keyword is 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 Class
Abstract classes are restricted classes whose objects cannot be created. You can only access an abstract class by inheriting it from another class. But you must be wondering: What's the use of a class if you can't create its object? Let's take an example to better understand it.
Why can’t we create an object of an abstract class?
An abstract class is largely undefined. It consists of abstract methods that are not implemented at all. An abstract class actually defines the blueprints of classes that will 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 :
Syntax:
In the above syntax, an 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 to understand Abstract Class - Click Here.
Abstract Method
An abstract method is an undefined method that 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 ,you just have to define abstract methods when the abstract class is extended.
How to create an abstract method?
The syntax for the abstract method is given below :
Syntax:
Let us see a Java program demonstrating the concepts we have learned so far.
Program:
Output:
Explanation:
- 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 that inherit 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 the abstract method display() to print professor data.
- Now, in the College class, we create the object of the classes that 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
Output:
Explanation:
- In the above program, we have an abstract class, School, inside which we have a constructor that 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 the alert variable.
- Now, coming to the AbstractEg class, we have made an object of the Student class which extends the abstract class School; we use that object to call the display() method printing Please pay the fees.
Abstract class containing overloaded abstract methods
Output:
Explanation:
- In the above program, we have an abstract class called School inside, for which we have two overloaded abstract methods named display().
- The first display() method does not contain parameters, whereas the second one takes one string parameter.
- The two abstract methods are overridden in the Student class, which inherits from the abstract class School.
- Now, coming to the driver class OverloadedAbstMethods, we have created an object of the Student class, which first calls the abstract method, which prints Admission is made.
- Then, while calling the second display method, we pass the string value Alert! Please pay the fees, which gets assigned to the string variable present in the second display() method, where we get both 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, inheriting from that class is impossible. Extending it to another class 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:
Output:
Now, let's look at the difference between the fiand abstract classesct class.
Abstract Class | Final Class |
---|---|
The abstract keyword is used to declare an abstract class. | The 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. |
Conclusion
- Abstract keyword is used in Java to achieve Data Abstraction in OOP.
- It is only possible to use abstract keywords with classes and methods in Java.
- We cannot instantiate an abstract class (though it can have constructors) as it can contain abstract methods that do not have a body.
- There is nobody 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 must also be declared abstract.
- Final classes cannot be inherited, and they cannot contain abstract methods.