Introduction
- Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information.
- Abstraction is selecting data from a larger pool to show only relevant details of the object to the user.
- It helps in reducing programming complexity and effort
- It is one of the most important concepts of OOPs.
How to achieve abstraction
- We can use abstract keywords in a class, method
- Abstract variables and constructors cannot be defined in Java
Rules for using abstract keyword
- In java if within a class we have a method for which the body is not there, then that method should be declared as abstract, but we should have the method signature
- If a class contains even one method as abstract then we should declare the entire class as abstract
- We can have both normal and abstract methods in a class
- The body is given by child classes
- We cannot instantiate, the object of the abstract class
- We can create the reference variables of the abstract class
- Final keyword members are not inherited hence, we cannot create abstract class with final keyword
Example Code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
children object =new children();
object.method_1();
}
}
abstract class abstraction {
abstract public void method_1();
}
class children extends abstraction{
public void method_1(){
System.out.println("abstraction achieved");
}
}