:::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.
abstract class class_name {
abstract void method_name();
}
:::
:::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:
abstract return_type method_name (parameters if any);
:::
:::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:
abstract class class_name {
abstract void method_name();
}
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}
// abstract class
abstract class School {
String alert;
// Protected constructor
School(String alert) {
this.alert = alert;
}
// Abstract method
abstract void display();
}
class Student extends School {
// Instantiate the parent abstract class
Student(String alert) {
super(alert);
}
// Override the abstract method
@Override
void display() {
System.out.println(alert);
}
}
public class ConstExample {
public static void main(String[] args) {
Student obj1 = new Student("Please pay the fees");
obj1.display();
}
}
Output:
Please pay the fees
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.abstract class School {
abstract void display();
abstract void display(String alert);
}
class Student extends School {
@Override
void display() {
System.out.println("Admission is done");
}
@Override
void display(String alert) {
System.out.println(alert);
}
}
public class OverloadedAbstMethods {
public static void main(String[] args) {
Student obj = new Student();
obj.display();
obj.display("Alert! Please pay the fees");
}
}
Output:
Admission is done
Alert! Please pay the fees
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:
//implementation of final class
final class One {
private int num = 12;
}
If another class inherits from the final class, it will cause a compile-time error
. The below code demonstrates this:
//implementation of final class
//it will cause compile time error
final class A {
private int num = 12;
}
//invalid
class B extends A {
public int val = 100;
}
public class Main {
public static void main(String args[]){
}
}
The above code will give you the following error:
Output:
Main.java:9: error: cannot inherit from final A
class B extends A {
^
1 error
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}
:::