Abstract Class In Java | PLM Developer

 
The Abstract is a partially implemented class in Java. The abstract class declared with the keyword abstract. If any class had at least one abstract method, then that class must be reported as an abstract class. An abstract class can contain both abstract methods and concrete methods. You can provide the implementation for abstract methods by inheriting the abstract class.
Any class extends the abstract class that should implement all the methods. The main objective of the Abstract is to achieve abstraction and hiding the implementation in Java.

Rules for Abstraction in Java

1. An abstract class can have abstract methods or non-abstract methods.
2. An abstract class must declare with an abstract keyword.
3. It cannot be instantiated.
4. It can have final methods.

Real-time scenario for Abstract class

If any class want to implement some of the methods in the interface but not all, In that case, we can use abstract classes to avoid unnecessary methods implementation in our class.

How to create an interface in Java

abstract class <CLASS_NAME> {
// variable 1
// variable 2
// method 1 {}
//method 2 {}
}

Java Abstract class Example

abstract class demoAbstract {
void fun1();
void fund2(){
System.out.println("Implemented Method in Abstract Class");
}
}
class Test extends demoAbstract {
public void fun() {
System.out.println("Hello");
}
public static void main(String args[]){
Test obj = new Test();
obj.fun1();
obj.fun2();
 }

Post a Comment

0 Comments