java9s.com
Subscribe:Posts Comments

You Are Here: Home » Core Java, OOAD » Abstraction in Object Oriented Programming

Abstraction in OOPs

Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object. Abstraction is separating the functions and properties that logically can be separated to a separate entity which the main type depends on.

This kind of Abstraction helps in separating the members that change frequently. Abstraction is one of the key principles of the OOAD(Object oriented analysis and Design). Applying Abstraction during the design and domain modeling, helps a lot in design the a system which is flexible and maintainable.

Abstraction is achieved by Composition.

Abstraction Example:

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 - Object Oriented Programming

Summary:

Abstraction is one of the fundamental principles of Object Oriented Programming languages.
It helps to reduce the complexity and also improves the maintainability of the system. When combined with the concepts of the Encapsulation and Polymorphism, Abstraction gives more power to the Object oriented programming languages.

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

© 2012 JAVA9S.com · Subscribe:PostsComments · Designed by Theme Junkie · Powered by WordPress