OOPS Programming Concepts Explained

 This Post will give you an idea on OOPS (Object Oriented Programming) Concepts. This might help one to face interviews related to software programming. To list, we have Abstraction, Encapsulation, Polymorphism, Overloading, Overriding, Inheritance, Aggregation, Association, Composition, Dependency. After reading this post you will get to know what Object Oriented Programming is famous for.

1.0. Abstraction:

Abstraction is the concept of hiding the internal details and describing things in simple terms (that is, providing only necessary details to the client).

For example, a method that adds two integers. The method internal processing is hidden from outer world. There are many ways to achieve abstraction in object oriented programming, such as encapsulation and inheritance.

Analogy to Abstraction: Car Brakes- You just know that pressing the pedals will stop the vehicle but you don’t need to know how it works internally.

Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. In the same way that abstraction sometimes works in art, the object that remains is a representation of the original, with unwanted detail omitted.

2.0. Encapsulation:

Encapsulation is the technique used to implement abstraction in object oriented programming. Encapsulation is used for access restriction to a class members and methods. Encapsulation is binding the data (variables) and behaviors (functions or methods) together in a single unit.

Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using private, protected
and public

For example, Class has attributes (i.e. data) and behaviors (i.e. methods that operate on that data).

Consider the below Class,

class Bag{     book;     pen;     ReadBook(); }

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

Encapsulation means hiding the internal details of an object, i.e. how an object does something. Encapsulation prevents clients from seeing its inside view, where the behavior of the abstraction is implemented. Encapsulation is a technique used to protect the information in an object from the other object. Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.

3.0. Polymorphism:

Polymorphism is the concept where an object behaves differently in different situations. Polymorphism means to process objects differently based on their data type.

In other words it means, one method with multiple implementation, for a certain class of action. And which implementation to be used is decided at run-time depending upon the situation (i.e., data type of the object).

Example, Lets us look at same example of a car. A car have a gear transmission system. It has four front gears and one backward gear. When the engine is accelerated then depending upon which gear is engaged different amount power and movement is delivered to the car.

Polymorphism could be static (compile time) and dynamic (runtime) both. Overloading is static polymorphism while, overriding is dynamic polymorphism.

Overloading in simple words means two methods having same method name but takes different input parameters. This called static because, which method to be invoked will be decided at the time of compilation

Overriding means a derived class is implementing a method of its super class.

Below Example will clarify the static (compile time) polymorphism in java.

public class Circle {
 public void draw(){
                System.out.println("Drwaing circle with default color Black and diameter 1 cm.");
        public void draw(int diameter){
                System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");
        public void draw(int diameter, String color){
                System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");

Here we have multiple draw methods but they have different behaviour. This is a case of method overloading because all the methods name is same and arguments are different. Here compiler will be able to identify the method to invoke at compile time, hence it’s called compile time polymorphism.

Below Example will clarify the Dynamic (run time) polymorphism in java.

package com.journaldev.test;
public interface Shape {
        public void draw();
package com.journaldev.test;
public class Circle implements Shape{
        public void draw(){
                System.out.println("Drwaing circle");
package com.journaldev.test;
public class Square implements Shape {
        public void draw() {
                System.out.println("Drawing Square");

Consider the above case where there is one Super (main) Class called “Shape” and its sub (child) Classes namely “Circle” and “Square”. All classes have common method named “Draw()”, but the sub-classes have “@override” option to implement run-time polymorphism. That means, the derived or extended classes implement their own method “Draw()” using “@override” keyword. If we are working in terms of super-class, the actual implementation class is decided at run-time. Compiler is not able to decide which class method will be invoked. This decision is done at run-time, hence the name as run-time polymorphism or dynamic method dispatch.

Below is the example of run-time polymorphism implementation.

Shape sh = new Circle();
Shape sh1 = getShape(); //some third party logic to determine shape

In above examples, java compiler don’t know the actual implementation class of Shape that will be used at run-time, hence run-time polymorphism.

4.0. Inheritance:

Inheritance is the object oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called super-class and the object that inherits the super-class is called subclass.

We use extends
keyword in java to implement inheritance. Below is a simple example of inheritance in java. In c#, “:” symbol replaces “extends” in java.

package com.journaldev.java.examples1;
class SuperClassA {
        public void foo(){
class SubClassB extends SuperClassA{                
        public void bar(){
public class Test {
        public static void main(String args[]){
                SubClassB a = new SubClassB();                

5.0. Association:

It represents a relationship between two or more objects where all objects have their own life cycle and there is no owner. The name of an association specifies the nature of relationship between objects.

Association is the OOPS concept to define the relationship between objects. Association defines the multiplicity between objects. For example Teacher and Student objects. There is one to many relationship between a teacher and students. Similarly a student can have one to many relationship with teacher objects. However both student and teacher objects are independent of each other.

Let’s take an example of relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own life-cycle. Both can be created and deleted independently.




6.0. Aggregation:

Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.

It is a specialised form of Association where all object have their own life-cycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.

Let’s take an example of relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.

7.0. Composition:

Composition is a special case of aggregation. Composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship can’t exist on it’s own, then it’s a case of composition. For example, House has-a Room. Here room can’t exist without house.

It is a specialised form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their life-cycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.

Let’s take an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete.

8.0. Dependency:

It represents a relationship between two or more objects where an object is dependent on another object(s) for its specification or implementation. This is represented by a dashed arrow.

Let’s take an example of relationship between client and service. A client is dependent on the service for implementing its functionalities.

Let’s take another example of relationship between a client and a supplier. A client is dependent on the supplier for supplying products. If the supplier will not supply the products, client cannot use those products.

9.0. References:

1. http://www.dotnettricks.com/learn/oops/understanding-association-aggregation-composition-and-dependency-relationship

2. http://www.journaldev.com/12496/oops-concepts-java-example

Leave a Reply

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