All About Abstraction
Abstraction – you can call it as generalization. Generalization means removing specific information to make that object represent a familiy of all similar objects. Obviously we leave specific things aside to consider them in specific objects, which may be derived from this general object, which is our abstract object. Let us take an example (you may be knowing this example already).
We have three objects – a car, a truck, a bus. By simple observation, we can tell that there is something common amongst them while there is something different which separates each one from other. We hold everything that is common amongst these object in another object (not really an existing object) called Vehicle. Before that let us detail out each object considered.
Car
- engine = car engine
- color = blue
- number of wheels = 4 + 1
- etc
- driving is possible
Bus
- engine = bus engine
- color = red
- number of wheels = 6 + 1
- etc
- driving is possible
Truck
- engine = truck engine
- color = blue
- number of wheels = 10 + 1
- etc
- driving is possible
If we take out common from above and create an object that represents all but does not exist on it’s own independently then it will be -
Vehicle
- engine=?
- color=?
- number of wheels = ?
- driving in general way
In this example, what we have done is nothing but generalization of specific objects to an object called as Vehicle. This vehicle represents all of them but none concretely. All objects can be constructed using Vehicle class. These can be the thumb rules for abstraction, i.e. any abstraction process should satisfy these rules.
1. Abstract/General type should represent a family of objects.
2. Abstract type may not exist on it’s own.
3. Each family object must of the abstract object type.
UML Context:
Abstraction can be represented using UML notations. We use (static) class diagram to represent the class structure.

Here is sample Java code showing abstraction.
Vehicle.java
public abstract class Vehicle {
private String engine;
private String color;
private int wheels;
public String getEngine() {
return engine;
}
public void setEngine(String aengine) {
this.engine = aengine;
}
public String getColor() {
return color;
}
public void setColor(String acolor) {
this.color = acolor;
}
public int getWheels() {
return wheels;
}
public void setWheels(int awheels) {
this.wheels = awheels;
}
public abstract void drive();
}
Car.java
public class Car extends Vehicle {
@Override
public void drive() {
System.out.println("I am driving a Car.");
}
}
Bus.java
public class Bus extends Vehicle {
@Override
public void drive() {
System.out.println("I am driving a Bus.");
}
}
Truck.java
public class Truck extends Vehicle {
@Override
public void drive() {
System.out.println("I am driving a Truck.");
}
}
Advantages of Abstraction:
1. Type definition and implementation are separated.
2. Common attributes and behaviour (methods) can be reused.
3. Reallife hierarchies can be represented using abstraction levels.
4. Frameworks and Patterns can be abstracted to provide skeleton for entire application.
Stable Abstraction Principle:
This principle defines stability of system/application packages using abstraction. ‘Stable packages should be an abstract packages.’ This means, instead of having too many concrete classes, there should be more abstract classes on which there is heavy dependency of concrete classes. Such packages are stable.
Where Abstraction can Go Wrong?
Some of the patterns where abstraction is used wrongly are listed below.
1. Many levels of abstraction to separate out behaviour instead of type.
2. Abstraction can be used only to increase reusability. If there is any common operation available, then an abstract class is created and those two classes are extended from this abstract class. This is done even if the two concrete classes are not from same family.
3. Sometimes developers get confused between use of abstract class and utility class, and type independent methods are also added to abstract classes.
What is Highest Level of Abstraction?
Having just a skeleton i.e. in Java it is creation of interface is the highest level of abstraction. Here we have just the contract defined and no scope for any kind of implementation.









Leave your response!