In this Java OOPs concepts article, we will learn four major object oriented principles– abstraction, encapsulation, inheritance, and polymorphism. They are also known as four pillars of the object oriented programming paradigm.
- Abstraction is the process of exposing the essential details of an entity, while ignoring the irrelevant details, to reduce the complexity for the users.
- Encapsulation is the process of bundling data and operations on the data together in an entity.
- Inheritance is used to derive a new type from an existing type, thereby establishing a parent-child relationship.
- Polymorphism lets an entity take on different meanings in different contexts.
The four oops principles are discussed in detail in the following sections.
Table of Contents Abstraction Encapsulation Inheritance Polymorphism
1. Abstraction
Abstraction in OOPs is very easy to understand when you relate it to the realtime example. For example, when you drive your car you do not have to be concerned with the exact internal working of your car. What you are concerned with is interacting with your car via its interfaces like steering wheel, brake pedal, accelerator pedal etc. Here the knowledge you have of your car is abstract.
In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics) while hiding away the implementation details.
In more simple terms, abstraction is to hide information that is not relevant or rather show only relevant information and to simplify it by comparing it to something similar in the real world.
Data abstraction is the way to create complex data types from multiple smaller data types – which is more close to real life entity. e.g. An
Employee
class can be a complex object of having various small associations.public
class
Employee
{
private
Department department;
private
Address address;
private
Education education;
//So on...
}
So, if you want to fetch information of a employee, you ask it fromEmployee
object – as you do in real life, ask the person itself.
2. Encapsulation
Wrapping data and methods within classes in combination with implementation hiding (through access control) is often called encapsulation in OOPs. The result is a data type with characteristics and behaviors. Encapsulation essentially has both i.e. information hiding and implementation hiding.
“Whatever changes, encapsulate it” – A famous design principle
Information hiding is done through using access control keywords (public, private, protected) and
implementation hiding
is achieved through creation of interface for a class. Information hiding gives the designer the freedom to modify how the responsibility is fulfilled by an object. This is especially valuable at points where the design (or even the requirements) are likely to change.
Let’s take an example to make it more clear.
Information hiding
class InformationHiding { //Restrict direct access to inward data private ArrayList items = new ArrayList(); //Provide a way to access data - internal logic can safely be changed in future public ArrayList getItems(){ return items; } } |
Implementation hiding
interface ImplemenatationHiding { Integer sumAllItems(ArrayList items); } class InformationHiding implements ImplemenatationHiding { //Restrict direct access to inward data private ArrayList items = new ArrayList(); //Provide a way to access data - internal logic can safely be changed in future public ArrayList getItems(){ return items; } public Integer sumAllItems(ArrayList items) { //Here you may do N number of things in any sequence //Which you do not want your clients to know //You can change the sequence or even whole logic //without affecting the client } } |
3. Inheritance
Inheritance is another important concept in object oriented programming. Inheritance in Javais a mechanism by which one object acquires the properties and behaviors of the parent object. It’s essentially creating a parent-child relationship between classes. In Java, you will use inheritance mainly for code re-usability and maintainability.
Keyword “
extends
” is used to inherit a class in java. The “extends
” keyword indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
A subclass inherits all the non-private members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
e.g.
class Employee { private Department department; private Address address; private Education education; //So on... } class Manager extends Employee { private List<Employee> reportees; } |
In above example,
Manager
is specialized version of Employee
and reuses department, address and education from Employee
class as well as define it’s own reportees list.4. Polymorphism
Polymorphism is the ability by which, we can create functions or reference variables which behaves differently in different programmatic context.
In java language, polymorphism is essentially considered into two versions:
- Compile time polymorphism (static binding or method overloading)
- Runtime polymorphism (dynamic binding or method overriding)
Above are four Java OOPs concepts, and I will suggest you to develop a good understanding of each one of it.
Happy Learning !!
No comments:
Post a Comment