Abstraction and Modularization

 

Abstraction:

Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object.  Abstraction in Java is achieved by using interface and abstract class in Java. An interface or abstract class is something which is not concrete, something which is incomplete. In order to use interface or abstract class we need to extend and implement abstract method with concrete behavior. One example of Abstraction is creating interface to denote common behavior without specifying any details about how that behavior works e.g. You create an interface called Server which has start() and stop() method. This is called abstraction of Server because every server should have way to start and stop and details may differ. As I said earlier Abstraction in Java is implemented using abstract class and interface as discussed in next section. In fact What is abstraction in Java, Difference between Abstraction and Encapsulation  is also a very popular core Java interview because strong OOPS skill is one of the primary requirement for Java developers.
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:
⦁ By using abstraction, we can separate the things that can be grouped to another type.
⦁ 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”.
⦁ 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.

Modularization:

Modular programming is the approach where the application code is subdivided into separate subprograms, with each running a specific function or subroutine. These chunks of code can be split up and worked on by multiple programmers, teams or by an individual over time, with each chunk of code dedicated to doing on specific task. These tasks also become recyclable, as they can be tweaked to fit into other programs. This keeps a programmer from having to write a similar task again in the future if a module already exists in another location that can handle the task at hand. The programmer can simply pull out an existing module, tweak it and insert it into the new program he/she is working on, saving time and energy in his/her work.
If creating a program to track attendance in multiple classes at a school, the master program would achieve this task for all students in all classes, throughout the year. If the application were to be broken down into modules, one module might track the attendance for a specific student over the course of the year. Another module might group the student attendance records by class, such as Geology or Science class, or even sophomore class, junior class, or the class of 2009. The modularization of the program allows it to be applied to various scenarios, students, years, schools, or anywhere attendance needs to be tracked, simply by tweaking the existing code and making it fit into the new scenario.

 

Comments are closed.