OOP Concepts

Abstraction:

  • Abstraction is the process of hiding the unwanted details and exposing only the essential features of a particular object or the concept of abstraction is used by classes and lists’ of attributes are defined in them. Abstraction lets you focus on what the object does instead of how it does.
  • 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 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:

  • 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 undergo 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. Whenever 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.

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.

Association:

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.

Example: A Student and a Faculty are having an association.

Aggregation:

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

Composition:

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Difference between aggregation and composition:

Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies which object contains the other object.

Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!

Encapsulation:

Encapsulation s a concept which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code. Encapsulation in Java is visible at different places and Java language itself provides many construct to encapsulate members.

Advantage of Encapsulation:

  • Encapsulated Code is more flexible and easy to change with new requirements.
  • Encapsulation in Java makes unit testing easy.
  • Encapsulation in Java allows you to control who can access what.
  • Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment.
  • Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place.
  • Encapsulation allows you to change one part of code without affecting other part of code.

What should you encapsulate in code?

Anything which can be change and more likely to change in near future is candidate of Encapsulation. This also helps to write more specific and cohesive code. Example of this is object creation code, code which can be improved in future like sorting and searching logic.

Design Pattern based on Encapsulation in Java:

Many design pattern in Java uses encapsulation concept, one of them is Factory pattern which is used to create objects. Factory pattern is better choice than new operator for creating object of those classes whose creation logic can vary and also for creating different implementation of same interface. BorderFactory class of JDK is a good example of encapsulation in Java which creates different types of Border and encapsulates creation logic of Border. Singleton pattern in Java also encapsulate how you create instance by providing getInstance() method. Since objects created inside one class and not from any other place in code you can easily change how you create object without affect other part of code.

Important points about encapsulation in Java:

  • “Whatever changes encapsulate it” is a famous design principle.
  • Encapsulation helps in loose coupling and high cohesion of code.
  • Encapsulation in Java is achieved using access modifier private, protected and public.
  • Factory pattern, Singleton pattern in Java makes good use of Encapsulation.

 

Polymorphism:

Dynamic binding / Static binding

Language Fundamentals:

Packages

Imports

Variable / Identifiers Declaration:

  • Identifiers/ variable names can start with an alphabetic letter, an underscore (_) and currency symbol ($).
  • After the first character variable names can contain numeric digits. Beginning of the name numeric not allowed.
  • Java bean method names must use camel notation. Depends upon the method’s purpose must start with set, get, is, add, and remove.

Declaration Rules:

  • A source code file can contain only one public class.
  • If the source file contains a public class file name must match with the public class name.
  • A file can have only one package statement and multiple import statements.
  • The package statement (if any) must be the first (non-comment) statement in the source file.
  • The import statements (if any) must come after the package and before the class declaration.
  • If there is no package statement, import statement must be the first (non comment) statement in the source file.
  • Package and import statements apply to all classes in a file
  • A file can have more than on non public classes.
  • Files with no public classes have no naming restrictions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s