Programming

OOP Concepts explained with real world scenario with Java code

Nisal Sudila
Technology Hits
Published in
4 min readDec 7, 2020

--

OOP concepts cannot be grasped without understanding these 6 core concepts in programming. They are,

  1. Class
  2. Object
  3. Encapsulation
  4. Inheritance
  5. Abstraction
  6. Polymorphism

1. Class

This is used to define objects. It’s basically used as a blueprint or a template to create objects, define their states and methods. So as to say states are variables/properties that define objects created by the class and methods are functions that the objects created by that class can do.

2. Object

This is an instance of that class with its states and methods. To create an object we use a constructor. Then with the newly created object, all the states and methods can be used.

Object instantiation with constructor and object accessing its methods

Real world example : Consider both class and object. When we consider an actual laptop, the factory or the mechanical system that manufactures the laptop is the class and the laptop produced is it’s object.

3. Encapsulation

In this concept it says to hide the variables of a class from external classes in the sense to prevent direct access to the variables, it should only be accessed by methods of the current class known as get and set methods. This is achieved through access modifiers which are public, private, protected and default.

Private keyword can restrict access to that component from outside the class, but grant access from inside the class. Public keyword grants access in both inside and outside of that class.

private used for variables to hide from other classes and public get and set methods used to access variables through class

4. Inheritance

A class gets to inherit all the states and methods of another class. A touch-screen laptop has mutual properties with a standard laptop. So a touch-screen laptop class could use the extends keyword to inherit the states and methods of laptop class.

Hence, Touch-screen laptop class is known as the sub/child class of super/parent class, Laptop.

Sub class implementation

There are multiple inheritance types that can be used in java. The below image might help you get an idea about how they are implemented with “extends” keyword.

3 types of inheritance of classes supported in Java with extends

Java doesn’t support multiple inheritance. In the sense you cannot have Class C inherit from both Class A & B, you can only do from either A or B. Although, interfaces can help you achieve multiple inheritance. Given you have Interface A and Interface B, you can “implement” both A and B to C.

Multiple inheritance achieved by using interfaces

5. Abstraction

Simply said, this concept is about providing information on what the object does while hiding the implementation(how the function is carried out) from the users. This is achieved using abstract classes and interfaces in java.

Interface and abstract class
Keyboard abstract class and interface implemented in Laptop
Methods called but functionality hidden in Laptop class
Shows how keyboard buttons clicked and laptop gets affected, while the user has no idea what the process of the button click does because everything is abstract to the keyboard.

Another example I will use is different books that has the same table of contents.

Abstraction example based on books

6. Polymorphism

This concept comes into play when there are a lot of classes that are related by inheritance. This basically lets the object take on many forms, when the parent class id used to refer the child class object.

In java there are 2 types of polymorphism. They are as follows,

i. Compile-time polymorphism (Method overloading)

ii. Run-time polymorphism (Method overriding)

Now exploring these 2 types with a real world example would be more helpful in grasping this concept.

i. Compile-time polymorphism (Method overloading)

This allows methods to have the same name inside a class, but the arguments passed inside the method are different. So you are able to call the same method name by an object, but it would choose which method to call by the arguments you pass into it.

Method overloading

ii. Run-time polymorphism (Method overriding)

2 child classes with different touchScreen() methods
run time polymorphism implementation

The above is an example how different methods with same name gets called by object with reference to parent class.

This whole series of real world examples with code summarizes everything about the core idea behind OOP concepts when implemented in java.

Hopefully this document helped you gain an idea to taking a step closer to better practices and basics. Thank you.

--

--