Abstraction in java

Let’s learn abstraction in java.

Abstraction in java

Data abstraction (abstraction in oops) is defined as showing necessary details and hiding the implementation details to the user.

Also read – inheritance in java

To create an abstract class in java and abstract method ‘abstract’ keyword is used. ‘abstract’ keyword is a non-access modifier.

Syntax of abstract class and abstract method

// abstract class
abstract class B
{
   
}

// abstract method
abstract void display();  // no body abstract method



Let’s see an example of abstract class where there are abstract methods and regular methods,

abstract class Car
{
   public abstract void carType();
   public void start()
   {
      System.out.println("car starts");
   }
}



In the above example we cannot create object of abstract class ‘Car’.

Car obj = new Car(); // error

In order to access abstract class Car, abstract class Car should be inherited from another class. Now let’s see example of abstract class that has an abstract method,

// abstract class
abstract class Car
{
   // abstract method
   public abstract void carType();
   public void start()
   {
      System.out.println("car starts");
   }
}

class Swift extends Car 
{
   public void carType() 
   {
      // body of carType()
      System.out.println("hatch back car");
   }
}

public class AbstractionExample 
{
   public static void main(String[] args) 
   {
      Swift obj = new Swift();
      obj.carType();
      obj.start();
   }
}



Output:

hatch back car
car starts

NOTE:

  1. We cannot create object of an abstract class. To access class members of an abstract class it must be inherited.
  2. On the other hand abstract methods does not have body and body is provided by inherited class or sub class and abstract methods can only be used in an abstract class.

Abstraction in java with realtime example: in an ATM machine when user inserts his card a list of ATM services is displayed namely, banking, mini statement, balance enquiry, quick cash, etc. etc.

Also read – interface in java

User doesn’t know what is running behind, what type of programming used, functionality involved and where the server is kept by bank where the data is collected and processed.

So a user just uses ATM to draw money from ATM machine. A user is not concerned about functionality offered by banks and hence hiding implementation details.

How to achieve abstraction in java?

We can achieve abstraction in java by abstract classes and interface.


Abstract class in java and abstract methods

Rules:

  1. We cannot create object of an abstract class.
  2. If a class consists of at least one abstract method then it should be declared as an abstract class.
  3. “abstract” keyword is used to declare an abstract class.
  4. An abstract class consists of both non-abstract method and abstract methods.
  5. There will be no implementation for an abstract method.
  6. Subclass should redefine method defined in an abstract class. Hence overriding definition of all abstract methods.
  7. An abstract class also consists of default and parameterized constructor.
  8. Abstract class may or may not have abstract methods.
  9. In an abstract class there can be concrete methods including abstract methods.

Advantages of abstraction:

  • increases security of a java application since only necessary information is provided to user.
  • code reusability.
  • reduces duplication of code.

Let’s see java program on abstraction,

// abstraction example
abstract class Shape  
{ 
    String color;
    // abstract methods 
    abstract double area(); 
    public abstract String toString(); 
      
    // abstract class constructor 
    public Shape(String color) 
    { 
    	System.out.println("Shape constructor"); 
        this.color = color; 
    } 
      
    // concrete method 
    public String getColor() 
    { 
    	return color; 
    }
}

class Square extends Shape 
{ 
    double side;
    public Square(String color, double side) 
    { 
        super(color); 
        System.out.println("Square constructor"); 
        this.side = side; 
    } 
  
    @Override
    double area() 
    { 
    	return side * side; 
    }
  
    @Override
    public String toString() 
    { 
    	return "Square color : " + super.color + " and area is : " + area(); 
    } 
}

class Circle extends Shape
{	  
   double radius;
      
   public Circle(String color, double radius) 
   { 
      // Shape constructor 
      super(color); 
      System.out.println("Circle constructor"); 
      this.radius = radius; 
   }
      
   @Override
   double area() 
   { 
      return Math.PI * Math.pow(radius, 2); 
   }
  
   @Override
   public String toString() 
   { 
      return "Circle color : " + super.color + " and area is : " + area(); 
   } 
}

public class AbstractDemo 
{
   public static void main(String[] args) 
   {
      Shape obj1 = new Square("Blue", 2.2); 
      Shape obj2 = new Circle("Green", 9);
      System.out.println(obj1.toString()); 
      System.out.println(obj2.toString());
   }
}



Output:

abstraction in java

Abstract method in java

Abstract method is a method without body. An abstract method has only signature. Actual implementation is determined by sub class or child class or regular class.

Also read – polymorphism in java

Suppose a class has an abstract method then it should be declared as an abstract class, the converse is not true. An abstract method can never be ‘final’.

Here’s abstract method in java with example,

// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);

   // non-abstract method 
   public void printValues()
   {
      System.out.println("Addition class printValues() method");
   }
}

class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }

   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }

   public static void main(String[] args) 
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}



Output:

12
21
Addition class printValues() method

Difference between abstraction and encapsulation in java

AbstractionEncapsulation
solves problem at design levelsolves problem at implementation level
shows necessary details and hides implementation details to the userhides code and data into a single unit
abstraction is about what object doesencapsulation is about how an object works
for example, abstraction is about car movingfor example, encapsulation is about how car is moving
abstraction is supported by interfaces and abstract classencapsulation is supported by access modifiers. protected, private and public