Object Oriented Programming

OOP concepts and its use

Kavindu Gayantha
Analytics Vidhya
8 min readMay 20, 2020

--

ref:https://static.javatpoint.com/images/java-oops.png

What is coming to your mind when you hear OOP.? Sometimes it looks like just three letters. Yes it’s true it’s three letters. But behind that there are set of concepts that every programmer should know.

OOP stands for object-oriented programming. Object oriented programming can be interpreted as a set of powerful concepts that tries to model real world objects into code. We consider about classes and objects and methods..etc when writing code with oop concepts. We need to be familiar with those words when coding.
So let’s look at what those powerful concepts are.

There are main four concepts in OOP. They are,

  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Now I am going to explain each of these things one by one.First of all we need to know some keywords. They are,

Class

A class is like a blueprint. And also it’s a logical definition. It layouts the basic structure for an object.

example of a class

according to this our class is Animal. So animals are representing the class of animal object. Inside the class the properties and the functions which is related to animal are located.

Object

Everything that exists in the real world is an object. And also an object is an instance of a class that models a real-world object. Once class may produce several objects as an example:-

from a house blueprint, we can build several houses similar but location and name are different.

animalObj is the object name of class Animal

This is how an object can be formed with a class in java. Everything in the class can access after making objects of that class.

Now let’s go to identify what the main concepts of oop are.

Inheritance

This is like inherit the same properties from generation to generation in the real world.

example:- animals make sound, but dog sounds and cats sound are different, and others also. That makes sound function comes from animals.

Now let’s take an example with code. This helps to avoid redundancy.

example of inheritance

Just think about an old handphone. We can get calls, send SMS, and sometimes we can listen to the radio only. But a new phone has more than those facilities. Camera, video, GPS.. etc. But it has functioned as the older phone was having like call, SMS, radio .. etc. So the new phone is having the same features as older phones that is inheritance.

Now let’s dive into inheritance with a code.

without inheritance

Same functions have repeated in both classes. But when we use the inheritance concept we can avoid that issue.

inheritance

In the concept of inheritance we have a doubt. If a new phone is inherited from old phone, some functions and properties of super class( old phone ) are different from sub class ( new phone ). So need to replace new phone’s features inside the new phone class. So we should be having a way to do that, shouldn’t we?. 😁

Yes of course there is a way , how to do that that is call method overriding. At the meaning of over ride you might feel something. 😉 If you don’t feel any sense, don’t worry I will teach it to you✌️ .

Method overriding

This means when we re-write a method that is the same as in superclass with a different body. example-:

consider as : man, cat, dog, inherits from animal class

Just think about eating function, commonly we use method eat() as the superclass. But there is a difference in eating of men and dogs, cats. So we can’t just inherit those methods and use them. We have to modify it as we want. Now consider about man class.

method overriding example

We rewrite the body of the method eat(); as suitable for man class. So that is the call method overriding. It is a very useful concept.

When talking about method overriding we can’t just leave method overloading. It’s also a simple concept but very useful as method overriding. Let’s take a look at it now.

Method overloading

This is also a very important concept. That means we make the same method several times inside a class.

But an error might be occurred when calling to that method because there are so many methods with the same name. So that could not happen. Can’t exist the same named methods several times.

Deffenately there should be a difference,shouldn’t it? You might have thought about the different 😉.

That is the parameter signature. It should be different in same-named methods. Parameter signature is something with the parameters. Just think about a method like this.

void method(int a){
//something here
}

In here parameters are integer type only. So the parameter signature is ‘ int a’

void method(int a, String s){
//something here
}

In this method parameter signature is int a , String s So you can see that it doesn’t matter to have same method name because it has different parameters. That is parameter signature.

Now let’s see an example.

example of method over loading

Like the above example, if we call method1() it is about a method in the 7th line. If we call a method1(5) it is about a method in the 10th line. If we call a method1(3,4), it is about the method in the 14th line. That’s it.

Polymorphism

Polymorphism means many forms of the same thing. Such as the same properties with a different method, same method with different properties.

The object of a sub class can assign to the super class reference and had many forms with super class reference. I mean many forms😉. That’s polymorphism.

There are two types of polymorphism.

  1. Static polymorphism as well as consider as compile-time polymorphism
  2. Dynamic polymorphism as well as consider as runtime polymorphism

Here there are two things with this concept that we should be knowing.

  • overriding- used in runtime polymorphism
  • overloading- used in compile time polymorphism

as an example. Just think like this.

There is a class Human. Inside it there is a function walk(), so we can override it after extending. (inherit)

One inherited function is “walking with hands-on forward” inside a subclass. That is the method overloading.
And another inherited function is “walking on foot in reverse” inside a subclass. That is method overriding.

The main difference between runtime and compile-time polymorphism is JVM identifies which method should run in which time like that and run it. But in the compile-time overloaded methods can be identified but overridden methods can’t be identified because the decision which method should run is decided at the run time. That is why it is called runtime polymorphism.

static binding- compile time polymorphism( different parameter signature )

It has the same methods but different parameter signature. It is method overloading.It is called compile-time polymorphism ( we can see the difference of the methods at first sight )

dynamic Polymorphism-runtime polymorphism

Here, the object of the subclass has assigned to the super class’s reference. When we call for vh.drive() it runs drive method in the car class.

Abstraction

Abstraction is used when we just use the function without knowing the mechanics( what is happening with a function ), and also ignore ill-relevant characters of an object. Abstraction is used for information hiding.

In real-world example:-

We know how to accelerate a vehicle, by pressing an accelerating pad. And also we know how to stop a vehicle by pressing brake pads. But we don’t know the mechanism of those function, we just use as we want. We don’t need the mechanism though. Just use those functions. That is an abstraction. Let’s see further.

another example:-

In a coffee machine we don’t know how coffee is making and what are the ingredients like that but we know when the button is pressed a cup of coffee can be got from the machine. ( we know the output but don’t know the mechanism ).

Now you might be having a doubt if the abstract method is not having a body how can hide information or how to write method body. 😉

The answer is simple. We can use method overriding technique to access abstract methods. We have to use inheritance and get super class’s abstract methods.

See the above example

abstraction

Advantages of Abstraction

  • Avoid repeating and increase re usability
  • Reduce complexity
  • secure data

Abstract class & Abstract methods

  • An abstract method should be in a abstract class but an abstract class should not be having only abstract methods
  • Abstract methods should not be having a body.

Encapsulation

Encapsulation means capsule properties and methods in one part. Like a medical capsule that covers medicines inside the capsule but we don’t know what are their ingredients and can’t see them and also we don’t have a bitter taste because of that. We see just the capsule.

Data hiding is the key feature in the connection of the encapsulation concept. We use access modifiers to implement encapsulation concept.

In the above example name, age are private. can’t access directly so we can use getters and setters to get and set values inside the capsule. getName(), getAge() are getters that use to get the values. And setName(), setAge() are setters that use to set values.

encapsulation

That is how we can use encapsulation with java.

I hope you got something from this article. That is what oop concepts and its use.

Happy coding !!

About the author

Kavindu Gayantha

Undergraduate

BSC(Hons) in Software Engineering

University of Kelaniya.

--

--

Kavindu Gayantha
Analytics Vidhya

Software Engineering final year undergraduate of University of Kelaniya, Sri Lanka