Object-Oriented Programming in Java

Akshay Tomar
May 9 · 4 min read

A method to solve up a problem or to do some task is termed as Paradigm. And to do some task with the help of a programming language in a specific way is called a Programming paradigm. There are many programming paradigms such as the Procedural Programming Paradigm, Logical Programming Paradigm and many more.

Object-Oriented Programming is a paradigm to solve a problem with the help of classes and objects.

Object: These are the key to understand the concept of Object-Oriented Programming. Our life is fully surrounded by objects such as a cow, pen, desk, car and many more.

An Object has two characteristics in the real world: state(things they have) behaviour(action they perform). For example: in real life dog is an object. The dog has states such as age, colour and breed and behaviour such as eat, sleep and bark.

Class: It is the blueprint from which individual objects are created. In the real world, we see many objects which belong to the same categories.

For example, there may be thousands of dog in the world and each of them has the same set of state and behaviour.

There are four basic building block of OOPs :

Inheritance: When one object acquires all the properties and behaviour of the parent object is known as Inheritance. The class that inherits is called a subclass(derived class) and the class from which it is inheriting is called a superclass(base class). It is done with the help of keyword extends. For example, all the properties of parents are inherited by their child.

class Baseclass{                
...
}
class Derivedclass extends Baseclass{
...
}

Java supports five types of inheritance in which single, multilevel and hierarchical can be achieved by classes and hybrid and multiple are achieved by an interface.

Abstraction: It is a process of hiding the implementation detail from the users and showing only the functionality to the users. In other words, users know only the external working without knowing how does it work internally.

For example in WhatsApp, we know only how to message someone, upload status and call but did not how these functions work internally. This is nothing but abstraction. We can achieve abstraction by using abstract classes and interfaces.

Encapsulation: The grouping up of all the variables and the methods into a single unit is termed Encapsulation. The main purpose of encapsulation is to achieve security.

It is a combination of data hiding and Abstraction. Data hiding is nothing but making all the variables inside the class as private variables so nobody from outside can access them and their values can be accessed with the help of getters and setters.

  public class Students{
private String name; // private data members
private int age;

public void setName(String name){ // setter
this.name = name;
}
public String getName(){ // getter
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}

Polymorphism: The word polymorphism is made up of two words ‘poly’ means many and ‘morphs’ means form. It means presenting a single thing in different ways. For example, if you are in a classroom at that time you behave like a student ,when you are in the market at that time you behave like an customer, when you are at your home at that time behave like a son or daughter. In this way, a single person shows many forms.

There are two types of polymorphism:

    public class Calc{

public int sum(int a, int b){
return a + b;
}
public double sum(int a, double b){ //overloading method
return a + b;
}
public double sum(double a, int b){ //overloading metod
return a + b;
public double sum(double a, double b){ //overloading method
return a + b;
}
}

2. Runtime polymorphism: It is also called Method overriding. In inheritance, if a subclass has the same method as declared in the superclass then it is called Method Overloading. It is done when the subclass wants to give its own specific implementation.

class Bank{     public double interestRate(){
return 2.0;
}
}
class SBIBank extends Bank{
public double interestRate(){ // overriding method
return 3.0;
}
}
class HDFCbank extends Bank{
public double interestRate(){ // overriding method
return 5.5;
}
}
class Account{
public static void main(String args[]){
Bank bank = new SBIbank();
Bank bank1 = new HDFCbank();
System.out.println("SBI interest rate:" + bank.interestRate());
System.out.println("HDFC interest rate:" + bank1.interestrate());
}
}
Output: SBI interest rate: 3.0
HDFC interest rate: 5.5

In the above code, the government has decided that all the bank has to give a minimum 2.0 rate of interest to their customers. HDFC and SBI offered their customers 5.0 and 3.0 as the rate of interest if they will open their account in their bank. So HDFC and SBI are overriding the interestRate() method of class Bank and giving their own rate of interest.

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store