OOPS Concepts in Java

Posted on Updated on

OOPS stands for Object Oriented Programming.

Objects refers to real world elements such as table, car, fan, etc. Object oriented Programming concept is used to define the objects in terms of classes and objects. Let us take an example of fan. A fan can be defined as class and it’s wings and rotating speed or any other functionality can be defined in terms of methods and parameters as wing size, motor capacity as variables. We see that a fan’s functionality and it’s definition can be implemented by directly dealing with object.

OOPS concepts is based on three principles.

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Encapsulation

Encapsulation is a mechanism that is used to bind code and data together and keeps it safe from outside interference and misuse. In a way we can say that it provides a protective layer over code and data and thus restrict it from being accessed by code defined outside the protective layer. This mechanism is also used to hide the complexity and implementation inside the class.

To encapsulate a class, private access modifier are used. Only the methods defined in that class can access and operate over data but not those defined outside it.

Let’s see an example to understand better-

Output

Inheritance

It is concept used to acquire the property of other object. This is important as it supports the concept of hierarchy classification. The hierarchical classification of data can be easily managed. The hierarchical classification helps us to defined only those methods/property which are required  and not defined in top/parent hierarchy.

Let’s see an illustration to understand better about the access modifier in term of inheritance.

Hierarchical classification of access modifier.

The inheritance interacts with encapsulation as well i.e. a class which is encapsulated is inherited then then class inheriting it will be access it’s data or methods. The key concept is to let the complexity grow linearly rather geometrically.

Let’s  see an example to understand better about it.

Parent class: InheritanceParent.java

Child Class: InheritanceChild.java

Output:

Polymorphism

Polymorphism means many forms. It’s feature of java which allows one interface to be used for general class of actions. The action is defined by nature of situations. The concept of polymorphism helps us to design a generic interface with group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action.

Let us understand this concept with a real life example. Suppose in case of dog who barks, when smells an intruder such as cat but same dog runs toward his bowl with smell of food. You see the only thing changing here smell type but class i.e. Dog remains same and implements different activities on basis of smell i.e. argument type.

This concept can also be explained by different constructor method. Suppose a class with two types of constructor. One with string argument and another with integer argument. This concept is not limited to different constructor method and can also be applied where constructor overloading is not required.

Let’s see an example to understand better about it.

Parent Class: PolymorphismParent.java

Child Class: PolymorphismChild.java

Output:

 

Stay tuned for more updates!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.