In this article, we will learn about OOP, its principle’s and their implementation in java.
Before just diving straight into Object-Oriented Programming let us first get familiar with the word object. In general terms, an object is something that can be seen and touched. And usually, all objects have some properties like smell, color, shape, etc which uniquely helps in identifying it.
For example, an object can be anything like your phone, car, pen, etc. Suppose we take a phone. It has a name, screen_size, processor, camera, and many other things that differentiate it. Like your phone has several properties and functions. Similarly, in programming, an object is defined as an instance of a class. A class is like a template for providing initial values for data fields and implementations of member functions.
Object Oriented Programming
Now after knowing what is an object let’s learn about OOP. In programming, there are different styles of writing a program. This style or approach of solving a problem using a programming language is called a Programming Paradigm. OOP is one of them. In object-oriented programming, we use classes and object to write code that is reusable, easy to maintain, and can be modified.
There are namely 4 principles of Object-Oriented Programming sometimes also called 4 pillars of OOP.
They are Inheritance, Encapsulation, Polymorphism, and Abstraction. Now we will see all of them in detail one
We are going to use Java as our programming language. If you use any other language of your choice such as C++, Python, C#, PHP, etc you can still read and understand the topics.
Inheritance allows one class to acquire features( data fields and methods) of another class.
The class which is derived or created from an existing class is called a subclass(also derived class, child class, extended class). The existing class from which a derived class is created is called superclass(also base class or parent class). The relationship between a parent and child class is an IS-A relationship, also called a parent-child relationship. it is denoted by an arrow from child class pointing towards parent class.
Using class in java there are three types of inheritance: single, multilevel, and hierarchical.
Now, let’s take an example from the real world. Human is our parent class having data fields like name, age, gender and method like sleep, eat. Now Engineer and Doctor are our two child classes which extend class Human. Now engineers and Doctors are also humans so they will have all the properties and methods of the Human, besides they will have some properties of their own. Like data fields such as emp_id, college_name, and a method like review_code for an engineer and data fields such as doc_id, address, and a method like examine_patient for the doctor.
In Java Inheritance is achieved using extends keyword.
Encapsulation is defined as bundling or wrapping of data fields and methods in a class. It is like a protective shield that prevents the data from being accessing and modifying by outer classes and code.
Here we are taking the example of a class Encapsulation_Example which has private data fields int age and String name. We have made a constructor that set values. There is also a default empty constructor that gets initialized if we don’t define our constructor. To access these private data fields and public methods we have to make an object of this class.
Polymorphism means “many forms”. Using Polymorphism one method can be performed in more than one way. There are 2 types of polymorphism one is runtime polymorphism(also called dynamic polymorphism) and the other is compile-time polymorphism(also called static polymorphism). Now we will see both one by one.
1. Runtime Polymorphism
In this type call to the overridden method is resolved at the run time. It is achieved by Method Overriding. Method Overriding means when a subclass has a definition has the definition of one of the member functions of the parent class. The parent class function is said to be overridden.
2. Compile Time Polymorphism
This type is achieved by Method Overloading. Method Overloading means when there are different methods with the same name but with different types of arguments or a different number of arguments.
Abstraction is the property in which only essential details are displayed to the user and other trivial and non-essential details are hidden. Suppose we take the example of a man driving a car. He knows how to drive a car but other functionalities like how actually car works, gear works, accelerator, and brake functioning doesn’t concern him.
In Java abstraction is achieved through Interfaces or Abstract Classes.
In Interface, we first define an interface using the interface keyword, which contains an empty body of methods that will be implemented in our class. Then we define a class which implements the interface.
In Abstract we define our parent class using abstract keyword and in it, we define empty method using abstract keyword whose body will be defined in the child class.
So finally we learnt that OOP (object-oriented programming) is a programming paradigm that is completely based on ‘objects’.
We also got to know what are it’s principles and how to implement them using Java.