Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 8, 2014 Categories: Object Orientation Concepts. 1 Comment on Object Orientation Principles

In this article we will look at the key object orientation principles also known as OOP principles. Although some people or textbooks mention three OOP principles, I would say there are four key principles namely abstraction, encapsulation, inheritance and polymorphism.

Note: OOP refers to Object Oriented Programming.

 

Abstraction

Abstraction is first of the object orientation principles. It is defined as hiding the complex details and presenting only with the relevant or necessary details. Abstraction is used to manage complexity while writing programs. Encapsulation and inheritance are used to implement abstraction while writing programs. Abstraction represents the external behavior of the object (what the object does) and encapsulation represents how it is implemented internally by the object.

As a real world example for abstraction let’s consider the car example.

abstraction car object orientation principles

 

When you want to purchase a car, the car dealer gives you the details of available colors, seating capacity, engine, gearbox, type of steering and other kinds of general details. The car dealer never tells you specific (complex) details of material being used for the hood, nuts and bolts and such.

 

abstraction car parts

 

The above details are not needed. These details are hidden by the car dealer. Like this, you can think of other examples in the real world. Abstraction is everywhere.

 

Encapsulation

Encapsulation is the fundamental principle to achieve abstraction in object oriented programming. Abstraction is conceptual and encapsulation is a way to implement abstraction.

Wherever you can find abstraction you can say that encapsulation is there but encapsulation may not always provide abstraction.

Encapsulation is defined as wrapping up or combining the data and code into a single unit. In the definition data refers to variables and code refers to methods (functions). Every object contains private data (variables) and public methods which allows other code to access the private data. Private methods are optional. We can view an object in OOP as shown below:

encapsulation oop

As a real world example for encapsulation let’s consider a television. We access the television by using a remote which provides an interface to communicate with the television. Television manufacturer is hiding its components (data) under the shell and is providing public interface (remote control) to access its components.

 

encapsulation television

 

In our real world example, television components are analogous to private member variables and public methods are analogous to buttons on remote control of the television.

Data hiding or information hiding is the side effect of encapsulation. Whenever we use encapsulation, we are implicitly hiding the data of an object which is known as data hiding.

Encapsulation helps programmers to write code which is extensible.

Note: Don’t worry about variables and methods. I will explain briefly about them in Java basics section.

 

Inheritance

Another way to implement abstraction is by using inheritance. In inheritance, classes are arranged as hierarchies. You might come across situations where even though objects have similar properties are quite different. In such cases, it is better to move all the common properties and behavior into one common class and the specific properties and behavior into special or specific classes.

Inheritance is defined as one object derives or inherits the properties and behavior of another object. As said above common properties and behavior are moved into a common class also known as super class or base class or parent class. Specific properties and behavior are moved into specialized classes also known as sub class or derived class or child class.

As a real world example, consider a child inheriting the properties (land and other assets) from their parents.

As another example, let’s consider that we are developing a program to work with geometrical objects like circle, rectangle etc. In the application I can create these objects and perform operations like move, re size etc. These operations are common for all the objects. So, we can use inheritance in this context. The resultant class hierarchy will be as shown below:

inheritance shape

 

Inheritance promotes re-usability.

 

Polymorphism

Polymorphism means many forms. More generally it is defined as one interface multiple implementations. It means that based on the situation or different factors, the same thing exhibits multiple behaviors or the same thing can be used to carry out different tasks. Here thing is used as a general element.

As a real world example for polymorphism, let’s consider the animal Chameleon. This animal changes its color to reflect the surroundings to hide from its prey. It is really cool! Based on the color of the surroundings the same thing (Chamelon) can change its color from one to another.

polymorphism chameleon

In every well written java program, we can find encapsulation, inheritance and polymorphism.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

the tutorial is cool, i mean very understandable

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up