Understanding Object-Oriented Programming and Its Applications

Mubashir Ibrahim
7 min readMar 22, 2023

Many of you have heard about the terms OOP, polymorphism, and inheritance. You probably studied these terms in your College or University Semester course. Many of you mastered these concepts, many are in confusion, and many are in self-doubt that whether they should continue programming or not……

The picture is taken from Dreamstime

Well worry not, in this article we will explore the concepts of Object-oriented programming. We will also explore topics around OOP concepts. We will try to answer basic questions and terms along the way, keep reading.
So what is Object-Oriented Programming aka OOP? Object-oriented programming is a popular programming paradigm, used in approximately every Complex Application.
Q:1: So here comes our first question: What is Programming Paradigm?
Answer: A Programming Paradigm is a set of programming practices, basically a way of programming or approaching Software Design and development to accomplish specific requirements.
The programming Paradigm helps write clean and organized code, which are indirect solutions for solving complex problems.
Here are some of the most popular Programming Paradigms other than Object-oriented programming:-

  1. Functional Programming
  2. Imperative Programming
  3. Logic Programming
  4. Concurrent Programming
  5. Event-driven Programming

We will explore the above paradigms in some other article(Comment if Yes). As for OOP concepts, let's dive into them.
Object-oriented programming is based on four simple concepts:-

  1. Abstraction
  2. Inheritance
  3. Polymorphism
  4. Encapsulation

Let's study each of these deeply and understand their use in real-world applications:-

Abstraction

Abstraction helps in hiding the implementation details of a class or method from the outside world. This way we can avoid the exposure of unnecessary information/data.
We achieve Abstraction by using Abstract Classes and Interfaces. A Class is only known to be Abstract if it has one or more abstract methods which are declared only and are intended to be implemented by child classes or sub-classes.
Now let's see Abstraction in Java with the help of a Coding example# 1.1:

public interface Shape {
public double getArea();
}

public class Circle implements Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

public class Square implements Shape {
private double side;

public Square(double side) {
this.side = side;
}

@Override
public double getArea() {
return side * side;
}
}

public class Main {
public static void main(String[] args) {
Shape circle = new Circle(27);
Shape square = new Square(18);

System.out.println("Circle area: " + circle.getArea());
System.out.println("Square area: " + square.getArea());
}
}

In the above example, we have 3 classes( Circle, Square, and Main ) and 1 interface( Shape ).
The Shape interface defines a method getArea() which is declared but not defined, which will return us an area of the shape. The Circle and Square classes inherit the Shape interface and override the getArea() method according to their size formula.
The Main class here creates instances of the Circle and Square classes. The Main class only knows that the Circle and Square objects are Shape objects and can return their area, but it does not know how the area is calculated. The implementation details of the getArea() method are hidden from the Main class and other classes that use the Shape interface.
So that’s how we implement Abstraction in Java. If you got any questions related to Abstraction, then comment your query in the Comments. Let's explore the next topic Inheritance.

Inheritance

Inheritance is used for deriving the parent class properties and methods in the child class. Inheritance in Java is a mechanism in which one class acquires all the properties and behaviors of a parent class. It is an important part of OOP. The idea behind it is that you can create new classes that are built upon existing classes.
For Example, if we want to access the variables and Method of the Parent class in the Child class then it will be done by using the “extends” keyword.
Java doesn’t support multiple inheritances. This means you can’t extend multiple classes in a single Class. But a single class can be extended by multiple classes. This Means Parent class can be extended by Child-1 class and Child-2 class at the same time.

public class Animal {
protected String name;
protected int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}

public void eat() {
System.out.println(name + " is eating.");
}

public void sleep() {
System.out.println(name + " is sleeping.");
}
}

public class Dog extends Animal {
private String breed;

public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}

public void bark() {
System.out.println(name + " is barking.");
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", 5, "Golden Retriever");

dog.eat();
dog.sleep();
dog.bark();
}
}

In the above-given example, the Animal class is the Parent class and the Dog class is the child class. The Dog class extends the Animal class using the extends keyword. The Dog class inherits two properties (name & age) and the eat() and sleep() methods from the Animal class.
The Dog class also has its field breed and its method bark(). In the constructor of the Dog class, the super(name, age) statement calls the constructor of the Animal class to set the name and age fields of the Dog object.
The Main class creates an instance of the Dog class and calls its eat(), sleep(), and bark() methods. The Dog object can access the inherited name and age fields and the eat() and sleep() methods from the Animal class as well as its breed field and bark() method.

Polymorphism

Polymorphism in object-oriented programming is a term that refers to the ability of an object to take on multiple forms or have multiple behaviors.
It allows multiple objects of different types to be treated as if they were the same type, enabling the program to be written that can uniformly work with various objects.
Polymorphism allows some of the key features of OOP which are Code reusability, extensibility, and flexibility. Let's talk about Polymorphism in Java, to achieve Polymorphism in Java we use Method Overriding and Method Overloading.
1. Method Overriding:
In Method Overriding we override the inherited method/function from Parent Class / Super Class. In the Child Class, we create functions with the same name but with different functionalities. The Method Overriding is also known as Run-time polymorphism. Let’s see Method Overriding with a coding example:-

class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}

class Dog extends Animal {
public void makeSound() {
System.out.println("The dog barks");
}
}

public class Main {
public static void main(String[] args) {

Animal animal = new Dog();
animal.makeSound(); // Output: The dog barks

}
}

Here we can the Animal Class, which has a method makeSound(). Similarly, the Dog class has a method with the same name makeSound(). This way we can override Parent Class methods and use the other remaining properties of Parent Class to our needs.
2. Method Overloading:
In Method Overloading, two or more methods with the same name are defined in the same class. The methods can be distinguished based on the number of parameters of each method. Method Overloading is also known as Compile-time polymorphism. Let’s learn Method Overloading with the example:-

public class Main {
public void addNumbers(int a, int b) {
System.out.println("The Sum of given numbers is "+(a+b));
}
public void addNumbers(int a, int b, int c) {
System.out.println("The Sum of given numbers is "+(a+b+c));
}
public void addNumbers(int a, int b, int c, int d) {
System.out.println("The Sum of given numbers is "+(a+b+c+d));
}
public static void main(String[] args) {

Main obj = new Main();
obj.addNumbers(2,3); //Prints The Sum of given numbers is 5
obj.addNumbers(2,3,4); //Prints The Sum of given numbers is 9
obj.addNumbers(2,3,4,5); //Prints The Sum of given numbers is 14
}
}

So in the above example, we can see that the Main Class has three methods with the same name addNumbers(), but all three of the methods have different numbers of parameters. That’s how we can define multiple methods with the same names but different numbers of parameters in order to achieve the desired functionality.

Encapsulation

Encapsulation is a term in object-oriented programming (OOP) which refers which is used for keeping the internal working hidden and operating data within a single unit(Class). Encapsulation is the practice of bundling data and providing a well-defined interface for interacting with the object.
Access Modifiers are used to achieve Encapsulation in Java (public, private, and protected). Using these Access Modifiers we can control the Data visibility and functions/methods within a class.
This way we can limit access to external code within our Class, ensuring its integrity, and internal state consistency. Let’s explore Encapsulation with the help of an example:-

public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

As we can see in the example, the variable name is of private access modifier, that’s why we cannot set its value directly. We can modify and use its value with the help of the setter and getter method, which can be as follow:-

Employee emp = new Employee("Mubashir");
System.out.println(emp.getName()); //Prints Mubashir
emp.setName("Mubashir Ibrahim");
System.out.println(emp.getName()); //Prints Mubashir Ibrahim

Here we used the emp.getName() method for accessing the value of the “name” variable. Similarly for modifying the value of the “name” variable, we used the emp.setName() method and updated the “name” variable value to “Mubashir Ibrahim”.

That’s enough for today, Do let me know about your suggestions in the comment section. I’ll try to answer your queries as soon as possible.
Thanks for reading, we will cover more topics in the future Insha Allah.

--

--