Object Oriented Programming Concepts in Java

Object Oriented Programming(OOPs) is a programming paradigm that implements concepts like Encapsulation, Inheritance, Polymorphism and Abstraction to solve a real world problem.

It tries to model real world entities as Objects. A programming is said to be truly object oriented programming language if everything is represented as Object. Data hiding, code re-usability, abstraction are some of the key features of OOPs.
Concepts of Object Oriented Programming Java

Simula is considered as the first object oriented language whereas SmallTalk is considered to be the first truly object oriented programming language.

Java is an object oriented programming language because it supports the fundamental features of object oriented programming like Inheritance, Encapsulation, Polymorphism and Abstraction.

Java, C++, C#, Python etc are examples of popular object oriented programming language.


Advantages of Object Oriented Programming
  • Object oriented programming helps us to write very large applications. OOP allows us to break an application onto multiple small manageable components whereas each component has specific responsibility. A large application written using OOps is much more easier to develop, maintain and extend in comparison with procedural programming.

  • It implements real life entities as Objects. It enables modelling real world complex systems into small software modules.

  • Code Reusability : We can extend an already existing class to add more features without re-writing everything from scratch. It saves lots of time as you have to write code only for the new feature you are adding, all old features will work as it is.

  • Abstract Interface : Internal complexities and implementation details are hidden from outside and other classes has a clearly defined interface.

  • Like real world entities, OOPs allows us to model objects into hierarchical order.

  • Encapsulation allows us to hide critical data from unauthorised external access. It enables us to expose only some information which is needed by other components .It adds an extra layer of protection.

Object

An object is a programming representation of an real world entity like car, dog, fan etc. All these objects have some properties and behavior.

Let's take an example of Car. Properties of a Car includes color, weight, engine type, manufacturer name etc and it's behaviors are brake, turn, startEngine, stopEngine, accelerate etc. We can model any real world entity in programming language in terms of it's properties and behaviors. As objects in OOPs represents real world entities, here properties of an entity is represented by member variables and behavior is implemented as member methods of a class. We will learn more about Objects in next tutorial.


Class

A class is a template or blueprint which is used to create objects. It specifies the schema of an object by encapsulating the properties and behaviors in a single entity.

Classes provides ability to define your own data types that correspond to real world entities like Car, Animal Player etc. A java class uses variables to define properties and methods to define behaviors of a real world entity. We will learn more about Classes in next tutorial.

Java Class

Encapsulation

Encapsulation is a mechanism of binding all data (variables) and the methods manipulating data into a single unit called Class.

The data of a class will be hidden from external classes, it is not directly accessible by external code. It can only be accessed through public methods of their class.

Encapsulation provides the security by hiding the internal data and implementation details from external code to avoid inadvertent changes. By calling public methods of a class, any external entity can access the data but cannot modify it.

Here are the benefits of encapsulation
  • Hiding the critical data from external components by making them private. Class have full control over what is stored and how, also how to expose the data to external world.

  • Hiding implementation details from clients. Rest of the world know how to access this information without knowing how it is calculated or managed.

  • We can change the data and internal implementation any time without affecting any client..as long as the we are not changing the interface of public methods.

How to achieve encapsulation in Java
  • Declare the member variables of class as private.
  • Expose some public methods for external world to interact with your class.
  • Keep you implementation details hidden inside private methods.

Inheritance

Inheritance is one of the most powerful features of Object Oriented Programming. Inheritance is a mechanism by which one object acquire some or all properties of another class.

The new class which inherits some features of existing class is known as subclass (or child class) whereas the existing class from which the child class is derived is known as superclass (or parent class). Multiple child classes can inherit from same parent class.

Java Inheritance
  • Using inheritance, we can extend the functionalities of a class in another class.
  • Using inheritance, we can define class in hierarchical order.

Inheritance is mainly used for code re-usability, you can reuse the field and methods of parent class and then add code for your specific requirement of child class.

Keyword extends is used by child class to inherit the properties of parent class.

Benefits of Inheritance
  • Code re-usability.
  • Hierarchical classification of objects.
  • Runtime polymorphism by method overriding.

Between a parent and child class, there exists a "is a" relationship, like "Apple is a Fruit". Here Apple class inherits some properties from Fruit class because an Apple is a Fruit.

Let's take a real world example. Let's define a class called Animal which contains three basic common properties of any animal which is color, length and weight.

class Animal {
   public String color;
   public float weight;
   public float length;
   
   public void walk() {
      // Code for walking 
   }
}

Now, to define a class for any specific animal let's say Tiger, we can extend "Animal" class and inherit some basic features of an animal add add some tiger specific features also.

class Tiger extends Animal {
   public boolean hasTail;
   public boolean isCarnivorous;
   public float runningSpeed;
   
   public void run() {
      // Do something
   }
} 

Here, Tiger class acquires all three fields of Animal class(because Tiger is an Animal) and defines three tiger specific field. In total, Tiger class has six features.


Polymorphism

Poly means many and morphos means forms, hence polymorphism defines the ability of the object to take many forms. In other words, polymorphism means one interface but different behaviour.

Using polymorphism, we can define a common method in parent class and then each child class can implement it's own specific version of the same parent method. Polymorphism is heavily used in inheritence to change the default behaviour of child class.

For Example, let's consider a parent class called "Shape" which contains a method called "calculatePerimeter()" to calculate perimeter of a shape. Now, we can define two different child classes "Square" and "Circle" which inherits from "Shape" class. As the formulae to calculate perimeter is different for Circle(2 X Pi X Radius) and Square(4 X Side), both Square and Circle can define it's own implementation of calculatePerimeter() method. This way, the internal implementaion of perimeter calculation logic of class Square and Circle is hidden from external components whereas providing a common interface calculatePerimeter() to get perimeter.

There are two types of polymorphism in java.

Method overloading (compile time polymorphism)

Here more than one methods have same names but different input parameters. They can either have different numbers of parameters or parameters of different types.

Method overriding (run time polymorphism)

Method overriding means a derived class is implementing a method of parent class. The method signature in derived class remains the same.


Abstraction

Abstraction in OOPS is the ability to hide the complexity and internal details from external world. Rest of the world just know about the expected behaviour of your class without knowing how it is achieved.

In other words, using polymorphism we expose what we do without informing how we do.

For Example, in a car we know what is the function of steering, gear shift lever, brake pedals etc, we know what will happen if I press/move this button/lever but we don't know about the internal mechanics and working of these auto components. We don't know how gear shifts happens internally, what are the mechanical components inside gear box etc.

Here internal complexities of mechanical components are abstracted out from driver. To drive a car, driver should only know about how to control a car using the controls provided.

You can change the internal implementation without affecting any clients as long as interface remains same.
For Example,
In a car you can replace a 3 cylinder engine with a 4 cylinder engine without impacting the ability of driver to drive this car as long as the car controls remains the same.

In java abstraction is achieved using Abstract classes and Interfaces.


Conclusion

Understanding object-oriented programming concepts is crucial for Java developers to design efficient, scalable, and maintainable software. Through classes, objects, inheritance, polymorphism, encapsulation, and abstraction, Java provides a robust framework for building complex systems. As you delve deeper into Java development, practice applying these principles in your projects to create more modular, flexible, and understandable code.