Abstraction in Java

Abstraction can be defined as the process of hiding the unwanted details and exposing only the essential features of a particular object or concept. The concept of abstraction is used by classes and lists of attributes are defined in them like cost, size and weight, and methods that operate on their attributes. Abstraction is also achieved through composition.

For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other. 
What is abstract class in Java?
Use the abstract keyword to declare a class abstract. An abstract class is something which is incomplete and you can not create instance of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be abstract in Java. 

Abstract Class Examples in Java –
Example 1:

abstract class Shape{
    abstract void draw();
}

class Rectangle extends Shape{
   void draw(){
     System.out.println("Drawing Rectangle");
   }
}

class Traingle extends Shape{
   void draw(){
     System.out.println("Drawing Traingle");
   }
}

class AbstractDemo{
   public static void main(String args[]){  
       Shape s1 = new Rectangle();
       s1.draw();
       s1 = new Traingle();
       s1.draw();
    }
}

Output:
Drawing Rectangle
Drawing Traingle.

Example 2:
A Car has Engine, wheels and many other parts. When we write all the properties of the Car, Engine, and wheel in a single class, it would look this way:

public class Car {
  
    int price;
    String name;
    String color;

    int engineCapacity;
    int engineHorsePower;
    
    String wheelName;
    int wheelPrice;
    
    void move(){
    //move forward  
    }

    void rotate(){
      //Wheels method
    }
    
    void internalCombustion(){
      //Engine Method
    }
    
}

In the above example, the attributes of wheel and engine are added to the Car type. As per the programming, this will not create any kind of issues. But when it comes to maintenance of the application, this becomes more complex.

Abstraction has three advantages:

  1. By using abstraction, we can separate the things that can be grouped to another type.
  2. Frequently changing properties and methods can be grouped to a separate type so that the main type need not under go changes. This adds strength to the OOAD principle -“Code should be open for Extension but closed for Modification”.
  3. Simplifies the representation of the domain models.

Applying the abstraction with composition,
 the above example can be modified as given below:

public class Car {
  
    Engine engine = new Engine();
    Wheel wheel = new Wheel();
    
    int price;
    String name;
    String color;
     
    
    void move(){
    //move forward  
    }
  }
public class Engine {
  int engineCapacity;
  int engineHorsePower;
  
  
  void internalCombustion(){
    //Engine Method
  }
  
}
public class Wheel {
  String wheelName;
  int wheelPrice;
  
  void rotate(){
    //Wheels method
  }
  
}

You can see that the attributes and methods related to the Engine and Wheel are moved to the respective classes.

Engine and Wheel are referred from the Car type. When ever an instance of Car is created, both Engine and Wheel will be available for the Car and when there are changes to these Types(Engine and Wheel), changes will only be confined to these classes and will not affect the Car class.

Abstraction in Java

Abstract Method :
Abstract methods are those which need to be implemented in subclass / child class. Abstract methods are only defined in superclass / parent class but with no body.

Syntax of Abstract Method:

abstract class clsName
{
// Variable declaration

// Constructor

// Method
abstract rtype mthName(params);
}

clsName is a valid identifier in java. It is a class name.
abstract is a keyword to define that method an abstract method.
rtype is return type of a method.
mthName is a method name and valid java identifier.

Example of an Abstract Method:

abstract class Shape
{
public static float pi = 3.142;
protected float height;
protected float width;

abstract float area();
}

class Square extends Shape
{
Square(float h, float w)
{
height = h;
width = w;
}

float area()
{
return height * width;
}
}

class Rectangle extends Shape
{
Rectangle(float h, float w)
{
height = h;
width = w;
}

float area()
{
return height * width;
}
}

class Circle extends Shape
{
float radius;

Circle(float r)
{
radius = r;
}

float area()
{
return Shape.pi * radius *radius;
}
}

class AbstractMethodDemo
{
public static void main(String args[])
{
Square sObj = new Square(5,5);
Rectangle rObj = new Rectangle(5,7);
Circle cObj = new Circle(2);

System.out.println("Area of square : " + sObj.area());
System.out.println("Area of rectangle : " + rObj.area());
System.out.println("Area of circle : " + cObj.area());
}
}

Output:
Area of square : 25
Area of rectangle : 35
Area of circle : 12.57

Declaring a method as abstract has two results:

  • The class must also be declared abstract. If a class contains an abstract method, the class must be abstract as well.
  • Any child class must either override the abstract method or declare itself abstract.

A child class that inherits an abstract method must override it. If they do not, they must be abstract,and any of their children must override it.
Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.

<<Previous <<   || Index ||   >>Next >>
Previous
Next

One Response

  1. Anonymous November 24, 2013