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.
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.
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:
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.
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 in detail 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 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.
In every well written java program, we can find encapsulation, inheritance and polymorphism.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Its not my first time to pay a visit this site, i
am browsing this web site dailly and obtain fastidious information from here everyday.
Object-oriented programming language provides us certain concepts which make our application development fast and easy.
There 3 most important features of OOPS
Polymorphism
Inheritance
Encapsulation
the tutorial is cool, i mean very understandable