How to Puzzle Out Object Oriented Programming Concepts

Nimesha Wijepala
5 min readNov 10, 2020

Introduction

Learning object oriented concepts is a must for a software developer. It is very easy to understand when you are able to see everything in the world as objects. We live in an object oriented environment where we can clearly see connections between objects and we give names for each different connection. Let’s dive into the deep.

Object vs. Class

Basically, a class is just a template or blueprint. We need a class to create objects of that kind. As an example let us take a car model. It is simply a class. We need a car model to create thousands of cars. Those cars are objects of car model class. We cannot use a car model to create a bus but only a car. It means that a car model contains unique characteristics that car objects can gain from it to appear and behave like a car.

Abstraction

When you go to a restaurant and order a meal you have no idea what is happening in the kitchen. Anyway, you get the meal which you ordered. You never know how much salt added, what ingredients are used, cooked temperature, and nothing but only the final result, your meal. This is simply the abstraction in object oriented programming. We use specific classes called abstract classes and interfaces to implement abstraction. Those classes and interfaces show only the resulting methods and variables but not all implementing details. We need another class to implement logics which appear in abstract classes and interfaces.

The main advantages of using abstraction in our code are reducing complexity and increasing reusability. With one abstract class or interface, it can create more classes with similar method names but different logic. A cook can create similar cooking methods but different meals.

Encapsulation

Doctors prescribe you capsules when you are ill and you have to take them to get cured. But can you separately see what is inside a capsule? No, it is just a mixed powder of several chemical compounds. We cannot separate them because they all are encapsulated into a single unit named capsule. Encapsulation in object oriented programming is similar to this.

Here we can get a single class as a capsule. To implement encapsulation, we need to keep inside data hidden from the outside classes. Keeping private variables and implementing public getters and setters to access those from outside, simply fulfill the mentioned. But what is the point of preventing direct access for variables? Well, there is a reason.

If we keep variables public, outside classes can access variables directly and easily, using variable names. But sometimes we need to add some implementation logic across variables. Here enters the actual purpose of encapsulation which is also called data hiding. See the java code snippet below.

public class Employee {
private int salary;
public void setSalary(int salary) {
int welfareDeduction = 0;
if (salary > 60000) {
welfareDeduction = 100;
} else {
welfareDeduction = 80;
}
this.salary = salary - welfareDeduction;
}
public int getSalary() {
return salary;
}
}

A company deducts some money for the welfare society from each employee. But that deducted amount should depend on the employee salary. It should deduct more amount from an employee who is paid above 60000 and less amount from an employee with a salary below 60000. Ok then, can you think of a way to implement this logic with direct variable access. No, it surely needs getters and setters, and the concept called encapsulation alias data hiding.

Inheritance

Let’s go to the zoo, pal. There are many different kinds of animals. Each kind of animal has appearance and behavior which is specific to that kind. But each one is an animal with common qualities like breathing, eating and etc. This is the object oriented concept of inheritance. See the below example.

Here we have a parent class and child classes. The parent class is Animal. Child classes are Duck and Lion. Both Duck and Lion have common qualities that inherit from its parent, Animal. Also, they have unique qualities that are specific to each.

To implement inheritance in your code, you have to extend parent classes and override parental methods as specific to the child class. See the code snippet below.

abstract class Animal {
public abstract void shout();
public void eat() {
System.out.println("eating given meal");
}
}
class Lion extends Animal {
public void shout() {
System.out.println("Grrrrr");
}
}
class Duck extends Animal {
public void shout() {
System.out.println("Quack Quack");
}
}
public class Tester {
public static void main(String[] args) {
Lion lion = new Lion();
lion.shout();
lion.eat();
Duck duck = new Duck();
duck.shout();
duck.eat();
}
}

Basically, we use inheritance in object oriented programming to increase reusability. We can use parental methods in child classes with or without modification. No need to rewritten.

Polymorphism

Action in many forms can be simply taken as polymorphism. It can be divided into compile-time polymorphism and runtime polymorphism. Let’s go through examples.

public class Adder {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
public static void main(String[] args) {
Adder adder = new Adder();
System.out.println(adder.add(1, 2));
System.out.println(adder.add(1.5, 2.8));
}
}

Here we need to add two numbers but those two numbers can be integers or doubles. To accomplish that, we can simply use the concept of method overloading. It is the same method with different parameters and return types. The compiler identifies the different forms of action thus called compile time polymorphism.

Runtime polymorphism comes with method overrides but at the runtime. In inheritance, we talked about how child classes access parental methods. Let’s understand the concept through the following example.

abstract class Animal {
public abstract void shout();
}
class Lion extends Animal {
public void shout() {
System.out.println("Grrrrr");
}
}
class Duck extends Animal {
public void shout() {
System.out.println("Quack Quack");
}
}
public class Tester {
public static void main(String[] args) {
Animal lion = new Lion();
lion.shout();
Animal duck = new Duck();
duck.shout();
}
}

Lion and Duck shout in two different ways which are unique to themselves. But both are derived from Animal class. Here it is a bit different than the previous inheritance example. We use the reference variable of the parent class for creating child class objects.

Animal lion = new Lion();
Animal duck = new Duck();

This concept is called upcasting in programming. In runtime, JVM identifies what animal is shouting and what sound should come out. The same action but appears in different forms using Method Overriding called runtime polymorphism. We get the benefit of increasing code reusability with polymorphism.

Conclusion

Well, we discussed the main object oriented concepts in a descriptive manner. It is always easier to remember concepts with living examples around us. Hope you will understand and enjoy the programming.

--

--

Nimesha Wijepala

One who work in software industry as well as loves writing. Trying to combine both for sharing knowledge all over the world.