Abstraction

Abstraction

Introduction

  1. Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information.
  2. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user.
  3. It helps in reducing programming complexity and effort
  4. It is one of the most important concepts of OOPs.

How to achieve abstraction

  1. We can use abstract keywords in a class, method
  2. Abstract variables and constructors cannot be defined in Java

Rules for using abstract keyword

  1. 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
  2. If a class contains even one method as abstract then we should declare the entire class as abstract
  3. We can have both normal and abstract methods in a class
  4. The body is given by child classes
  5. We cannot instantiate, the object of the abstract class
  6. We can create the reference variables of the abstract class
  7. 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");
    }
}