Explain OOP Concepts With Real Time Examples.

Object-oriented programming System(OOPs) is a programming paradigm based on the concept of "objects" that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Object oriented programming brings together data and its behaviour(methods) in a single location(object) makes it easier to understand how a program works.

Object: Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code.

Class: A class can be defined as a blueprint from which you can create an individual object. Collection of objects is called class. It is a logical entity.

Inheritance: When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

The keyword used for inheritance is extends as follows.

class derived-class extends base-class { //methods and fields }
class SuperClassA {
	public void foo(){
		System.out.println("SuperClassA");
	}
}
 
class SubClassB extends SuperClassA{
	public void bar(){
		System.out.println("SubClassB");
	}
}
 
public class Test {
	public static void main(String args[]){
		SubClassB a = new SubClassB();
		a.foo();
		a.bar();
	}
}

Here SubClassB inherited the method foo() from SuperClassA.

Polymorphism: Polymorphism means taking many forms, where ‘poly’ means many and 'morph' means forms. Polymorphism allows you define one interface or method and have multiple implementations. In Java, there are two types of polymorphism: compile time polymorphism and run time polymorphism. Compile time (static) polymorphism occurs when a method is overloaded; that is, when the argument used with the method is changed. This allows us to have more than one methods with same name in a class that differs in signature.
class StaticOverloading
{
    public void fun(char c)
    {
         System.out.println(c);
    }
    public void fun(char c, int num)  
    {
         System.out.println(c + " "+num);
    }
}
 
public class ExampleOverloading
{
   public static void main(String args[])
   {
       StaticOverloading obj = new StaticOverloading();
       obj.fun('a');
       obj.fun('a',10);
   }
}

Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather.

class Animal{
   public void sound(){
	System.out.println("Default Sound");
   }
}
public class Dog extends Animal{
   public void sound(){
	System.out.println("Woof");
   }
   public static void main(String args[]){
	Animal obj = new Dog();
	obj.sound();
   }
}

Abstraction: Hiding internal details and showing functionality is known as abstraction. Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is. In Java, we use abstract class and interface to achieve abstraction.

Encapsulation: Binding (or wrapping) code and data together into a single unit is known as encapsulation. A java class is an example of encapsulation. It also protects the integrity of the data – prevents it from being needlessly altered by restricting access to the data, preferably by hiding it from outside elements. Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding. Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.

Hope you have enjoyed reading this post. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.