:::section{.main}
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.
:::
:::section{.main}
:::
:::section{.main}
Before using the abstract keyword in Java in your code, you must know some rules. There are some important do's and do n'ts for using abstract keywords.
abstract
keyword cannot be used when the' final' keyword is used.private
.static
.:::
:::section{.main}
An abstract method is an undefined method, the type of 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.
The syntax for the abstract method is given below :
Syntax:
:::
:::section{.main}
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 have a question.
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), which only allows derived types to initialize it.
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 ,above example, let's create a Java program to understand the abstract class.
:::
:::section{.main}
Output:
Explanation:
alert
.display()
declared inside the abstract class.constructor
for the Student class, which calls the super
method with a string.display()
method is implemented in the child class Student
, which prints the value of the alert
variable.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.Output:
Explanation:
School
inside, for which we have two overloaded abstract methods named display()
.display()
, the method does not contain parameters, whereas the second one takes one string parameter.Student
class, which inherits from the abstract class School
.OverloadedAbstMethods
, we have created an object of the Student
class, which first calls the abstract method, which prints Admission is made.display()
method, where we get both the strings as output.:::
:::section{.main}
:::
:::section{.main}
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:
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. |
:::
:::section{.summary}
:::