Encapsulation, Inheritance & Polymorphism - OOP principles

The three OOP principles

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

1. Encapsulation

➡ The wrapping up of data and Methods into a single unit (called class) is known as Encapsulation.
It is also known as information hiding concept.


The methods inside the class can only access the data present in that class.

It is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interface and Misuse.

Let's take a simple mobile phone. It is a complex electronic device, which is used for text and voice communication. The user of that mobile does not know the complexity of that mobile phone. He is no concerned about how it works. He is only concerned about is the fact that by pressing the green(answer) key, he can talk to the person on other side.
Here user is isolated from all the the complex circuitry which is well encapsulated within the mobile phone.

➡ Methods in java is near about same as the functions in c programming language.

A sequence of process steps can become a collection of message between these objects,

purpose of a class is to encapsulate complexity and to provide security.

There are public and private methods.

Public methods are accessible throughout the program, while private methods are accessible to that class only. they are not accessible outside the class in which they are declared.

2. Inheritance

It is the process by which objects of one class acquire the properties of objects of another class.

➡ It supports the concept of Hierarchical classification.

It provides the the idea of reusability. It means we can add additional features to an existing class without modifying it. 

It allows programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any undesirable side effects into the rest of the classes.

➡ The derived class is known as 'subclass'.

eg, a child inherits the traits of his/her parents.
Another example is of birds

3. Polymorphism

Polymorphism means the ability to take more than one form.

Polymorphism in Java is a concept by which we can perform a single action in different ways. Thre are two types of polymorphism in Java: Compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method oerloading and method overriding which we will discuss later-on in our syllabus.


Keep enjoying.

Make sure you subscribe to the blog.

Comments

Anonymous said…
Amazing explanation with illustrations. Liked it.

YouTube

Popular posts from this blog

GTU OOP Programs - 11

Mini project ideas for Operating System

GTU OS Program - 8