Object-Oriented Programming In Java

Nishajha
7 min readFeb 8, 2021

Object Oriented Programming is an engineering approach to build software.Object Oriented Programming uses the concept of object and classes to model the real world entity.It allows user to create objects they want and create methods to handle those objects.

Every real-world object can be described by the properties they posses .Let us consider “car” as an object.We can tell that model,color,price etc are some of the properties in order to describe a car.OOPS facilitates utilizing and creating reusable software component.OOPS was introduced to overcome flaws in the procedural approach .

Objects and Classes

Object is real world entity which has states and behavior.Behavior of an an object is an action performed by an object.Object is a physical entity created using class.Consider an example of online banking user object.The user will have account number,balance,contact number etc associated with his account which is the state.The behaviour can be transfer money,view balance,pay bill,login to the account etc.

Class is logical entity that is used to create an object .Class is considered as blueprint using which we can create objects.Multiple objects can be created using class.Multiple objects can be created using class is called identical or similar object.Since object is created using class it is called as “instance of the class”. Class doesn't occupy space but objects have address i.e. occupy a space.Every objects works independently i.e. when an object is modified or destroyed then it doesn't affect another object or class.

Class and Object Example

The Four Pillars Of OOPS

Java follows a class-based object-oriented programming that revolves around the object concept.OOPS concepts intend to improve code readability and usability by structuring the program in efficient manner.The principles of object-oriented programming are:

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction

Lets look at these object-oriented programming principles in details one by one to get understanding of what they actually do.We will use java as programming language for code examples so that we have better understanding of Oops concepts.

  1. Inheritance

Inheritance is a process or mechanism of acquiring properties or members of one class into another class.We can achieve inheritance by using the keyword “extends”. Inheritance is basically achieved by constructor chaining i.e. a subclass calling super class constructor hence acquires the properties of super class.

Uses of Inheritance:

  1. We can avoid code redundancy.
  2. We can reuse code many times.
  3. We can achieve generalization.
  4. Multiple inheritance is not allowed in java(a class can’t extend more than one class).

NOTE:Using Inheritance we achieve tight coupling because any change in super class will also affect the sub-class.Inheritance is also known as IS-A relationship.Final class cannot be inherited.Private members cannot be inherited.

We will try to understand the concept of Inheritance b looking at the following diagram to get a clear picture.

Inheritance Example

In the above diagram card is a base class .Forex card and Credit card are the derived classes of card class.Following is a sample code for the above example.

Types of Inheritance:

  1. Single-level Inheritance

A class (derived/child)inheriting properties from a single (parent/base)class,then it is single-level Inheritance.

Single-level Inheritance

2.Multi-level Inheritance

A class inherits properties from a class which again has inherited properties from another class is multi-level inheritance.

Multi-level Inheritance

3.Hierarchical Inheritance

When a single class (base class) acts as parent for multiple classes (derived)i.e one class serves as super-class for more than one sub-class .

Hierarchical Inheritance

4.Multiple Inheritance

When a class is derived from two or more parents it is said to be multiple inheritance.

Multiple Inheritance

5.Hybrid Inheritance

Hybrid Inheritance is a combination of multiple and hierarchical inheritance.

Hybrid Inheritance

Note:Multiple and hybrid Inheritance is not possible with class.It is only possible using Interface.

2.Polymorphism

Polymorphism is ability of an object to take multiple forms.Allows us creating an entity that will perform different operations in different conditions.It allows us to create consistent code.In simple,one action and different reactions is polymorphism.The reaction depends on object to object.

Method Binding:-The process of associating the method logic or definition to its method call.There are two types of binding:

  1. Early binding:-In this case call to a method is resolved at compile time i.e. compiler takes the binding decision.
  2. Dynamic Binding:-In case of run-time or dynamic binding,call to a method is resolved at run-time i.e.binding decision is taken by JVM(usually in case of method overriding).

We can achieve polymorphism in two ways:

1.Run-time or Dynamic Polymorphism

2.Compile-time Polymorphism

Run-time or Dynamic Polymorphism:

It is the ability of the object to behave differently based on the object that is acting upon.Run-time or Dynamic Polymorphism is achieved by using method overriding.

Note:In case of run-time polymorphism ,call to a method is resolved at run-time.

Method Overriding:-It is the process of providing sub-class specific method implementation for an inherited method.Inheritance is mandatory for method overriding.When a method gets inherited from super class to subclass,then in the sub-class we can change the method logic or definition.Optionally we can use ‘annotation’. Static methods cannot be overridden.

Method Overriding Example

Compile-time Polymorphism:

In java,compile time polymorphism is achieved by method overloading.In this case call to a method is resolved at compile time.

Method Overloading:-In a class having more than one method with same name but change in signature is called method overloading.Method overloading is different forms of doing same activity.Method overloading is related to compile time polymorphism.For example,

Method Overloading Example

3.Encapsulation:

It is a process of wrapping up of the data members along with its data handler method (getter and setter methods). It is also called as data hiding.It hides the complexity.

Advantages:

  1. We can perform data validation and data filtration.
  2. We can avoid unauthorized access of data.
  3. Data can be made read only or write only.

Steps to achieve Encapsulation(Specification of Java Bean):

  1. Class must be public and non-abstract.
  2. Data members must be private.
  3. Each data members must have public getter and setter method.
  4. The class must have public default constructor.

Note:

  1. setter and getter methods must be public.
  2. setter must be void and must take parameter.
  3. getter method must have return-type and must not take any parameter.
Encapsulation Example with code
Example for read-only
Example for write-only

4.Abstraction

Abstraction is a way of hiding the internal implementation and showing only the features user wants to know about.Java provides two ways to implement abstraction:abstract classes and interfaces.

Abstract Classes:

Abstract class is an incomplete class which may have both abstract and concrete methods.Abstract class is just like any other regular class.Abstract class can have super class and also sub-class.An abstract class is a super class(parent class) that cannot be instantiated i.e cannot create an object of abstract class.It is necessary to inherit abstract class in subclass to make an object.Abstract classes are defined with “abstract”keyword.Abstract classes contain both concrete as well as abstract methods.Abstract method contains only method signature,while concrete methods declare a method body as well.

Note: Abstract method cannot be private,final and static.Overriding a abstract method is optional but over-ridding a abstract method is compulsory.

Abstract class Example

Interface:

There are three meanings of an Interface-

  1. It is an intermediate between the service and consumer.
  2. It is an 100% abstract class.
  3. Rules repository/coding contract.

Programmatically,Interface is declared by using keyword interface.It can have only static,final and public fields and abstract methods.Java interfaces allow us to implement multiple inheritance in our code,as a class can implement any number of interfaces.

A class can inherit an interface by using “implements” keyword.The sub-class of an interface is called implementation class.When a sub-class implements an interface then it is mandatory to override all the abstract methods otherwise compilation error.We cannot create am object of interface but interface is used as an intermediate or reference to refer the sub-class object.

Interface Example

Conclusion:

OOPS helps in modulating for easier troubleshooting so that we can very easily figure out where things are not working properly.We can achieve reuse of code through Inheritance thus achieving the DRY principle.OOPS gives us the flexibility through polymorphism.OOPS helps us model real-worlds object into a programming construct.

--

--