Abstraction in java

Let’s learn abstraction in java.

Abstraction in java

Abstraction is defined as showing necessary details and hiding the implementation details to the user.

For 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.

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

So a user is not aware of all these stuffs. Only functionality offered by banks is shown to the user and hiding implementation details.

We can accomplish abstraction using abstract classes and interface.


Abstract class and abstract methods

  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 abstract class.
  3. “abstract” keyword is used to declare an abstract class.
  4. An abstract class consists of non-abstract and abstract methods.
  5. There will be no implementation for an abstract method.
  6. Subclass should redefine method defined in 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. 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