Object Oriented Programming With Java

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

OOP Principles

Inheritance

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.

types of Inheritance in Java

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

Polymorphism

1. Runtime Polymorphism

2. Compile Time Polymorphism

Abstraction

1. Interface

2. Abstract

Conclusion

We also got to know what are it’s principles and how to implement them using Java.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store