The Four Pillars Of OOP
Let us discuss what is oop ?
oop refers to object-oriented programming.This concept is being used for a more than a decade and its popular among most of the popular programming languages like java,Smalltalk,c++ and many more.
Now question arises what is object,Object is like an entity which exist in real world and we can very much relate this to real world objects.Certain Object has some properties and exhibit some behaviors. for example consider a dog, a dog has some properties like legs,color,type of dog and also perform some actions like bark,run,eat etc.
Here we are going to discuss this oops concept and its uses in Java.
The four main pillars of the oop concepts are:
Inheritance defines as when a class acquire all the properties of another class that means all the methods and data members are acquired by the class.And in java this can be done by using extends keyword.The class from which we acquire properties is called “parent” class and the other one is called “child” class.This established a “is-a” relationship between the class.We can relate this better with parents and their children example like a child get the genes passed on from their parents like eye color,height etc.
This can be easily understand with the following example,
Here we can clearly see that “PhysicsTeacher” class extends “Teacher” class and inherited the properties, here PhysicsTeacher is child class and Techer is the parent class.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit or we can say that binding the data with methods.It provides more controlled access over the data.
In encapsulation the variables are not visible to the other class and can only be accessed through the current methods only.
Encapsulation can be achieved by,
- declaring the variables of the class as private
- use of setter and getter to access the variables
Here we can see that using encapsulation we are providing a more controlled access to the data of “Books” class.
Abstraction is a process of hiding the implementation details and showing only the functionality to the user.
Its very much relatable to our real life like we all use ATM machines for cash withdrawal,money transfer and printing mini-statement and many more things without knowing how the ATM works internally.
Abstraction can be used to provide more security to the data from unauthorized methods.
This can be achieved by using abstract classes and interfaces.An abstract method does not have a body.
Here we can see that in “Plane” class the methods does not have body but still we can call those methods and execute them.we can use interface also here and rather than extending the class we can simply implement them and get the similar result.
In simple words it means “many forms” , In object oriented programming language like java we can say polymorphism is a concept by which we can perform a single action in different ways.
we can find this polymorphism in our real world like a man working in a school.For students of the school that man is a teacher but at home he may be someone’s son, may be a husband, can be a father etc…
Polymorphism is actually of two types:
Here we can clearly see the run method in Vehicle class is being overridden in Bike class.
oops concepts are really good when it comes to solve real world problems that is why it gained its popularity so fast.