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.


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 where modes has specific responsibilities. 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 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.

Simula was the 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.


Encapsulation

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

In encapsulation, 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 class can access the data but cannot modify it.

Here are the benefits of encapsulation
  • Hiding the critical data from external interference by making them private. Class have full control over what is stored and how.

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

  • We can change the data and internal implementation any time without affecting any client..as long as the we are not changing the prototype 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.
For Example:
public class SimpleInterest {   
    private int principal;
    private float rate;
    private int time;
    
    // Constructor to set variable of object
    SimpleInterest(int principal, float rate, int time){
        this.principal = principal;
        this.rate = rate;
        this.time = time;
    }
    
    // Public method to get the interest amount 
    public float getSimpleInterest(){
        return (principal * rate * time)/100; 
    }
}

In SimpleIntereset class, we made all member variables as private to prevent any direct external access. However, external clients can call getSimpleInterest public method to know the simple interest.


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.

  • 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. 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 from fruit class.

Let's take a real world example. Let's define a class called Animal which contains some basic properties common to all animal like color, length and weight.

class Animal {
   public String color;
   public float weight;
   public float length;
   
   public void walk() {
      // Do something
   }
}

Now, to define a class for any specific animal let's say Tiger either we can create a new Tiger having general fields like color, size and weight of it's own or we can extend "Animal" class and add some tiger specific fields.

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 and defines three tiger specific field. In total, Tiger class has six properties.


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.

There are two types of polymorphism in java. In polymorphism, the decision of call version function is taken during runtime instead of compile time. Polymorphism is heavily used in inheritence to change the default behaviour of child class.

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 clients and expose a simple interface to clients. Here, 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 does without informing how we does.

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 how gear shifts happen, 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.

Another advantage of abstraction is that, you can change the internal implementation without affecting any clients as long as interface remains same.
For Example,
In a car you can change a 3 cylinder engine by 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.