Object-Oriented Programming in Java

Object-Oriented Programming in Java

·

3 min read

OOP focuses on the objects that are required to be manipulated instead of logic.

  • It makes development and maintenance easier
  • It provides data hiding
  • It provides the ability to simulate real-world examples
  • less memory and organized
  • reusable

Abstraction

It is a process of hiding implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it. Let's see an example from the following code.

public class Enemy {
    public void Talk() {
        System.out.println("I am an enemy");
    }
}
public class Main {
    public static void main(String[] args) {
    // write your code here
        Enemy e = new Enemy();
        e.Talk();
    }
}

Here you can see we only care about calling the Talk method not about the underlying implications.

Inheritance

It is used to define the relationship between two classes. When a child class acquires all properties and behaviors of parent class known as inheritance. The child class can reuse all the codes written in the parent class. It provides the code reusability.

I have an Enemy class and a Vampire class which extends functionality from the Enemy class. Let's have a detailed look at it.

public class Enemy {

    int health;

    public void Talk() {
        System.out.println("I am an enemy");
    }
}
public class Vampire extends Enemy{

}
public class Main {

    public static void main(String[] args) {
    // write your code here
        Vampire vampire = new Vampire();
        vampire.health = 10;
        vampire.Talk();
    }
}

You can see even though our Vampire class has no methods or variables defined, when we call its object in the Main method it has both Talk() method and health variable available.

Polymorphism

It is the ability of an object to behave in multiple forms. The most common use of polymorphism is Java when a parent class reference type of variable is used to refer to a child class object.

Let's have a look at the following java classes,

public class Enemy {

    int health;

    public void Talk() {
        System.out.println("I am an enemy");
    }
}
public class Vampire extends Enemy {

    public void Talk() {
        System.out.println("I want to suck your blood");
    }
}
public class WareWolf extends Enemy {

    public void Talk() {
        System.out.println("I'm gonna bite you");
    }
}
public class Main {

    public static void main(String[] args) {
    // write your code here
        Vampire vampire = new Vampire();
        WareWolf wareWolf = new WareWolf();

        Enemy enemy = vampire;
        enemy.Talk();

        Enemy [] enemies = { vampire , wareWolf };
        enemies[0].Talk();
        enemies[1].Talk();
    }
}

You can from the main we can create instances of different classes and make them exist in different forms and they work perfectly.

Encapsulation

We can hide direct access to data by using private key and we can access private data by using the getter and setter method. Having a look from the following code will help us understand more about it.

public class Enemy {

    private int health;

    public void Talk() {
        System.out.println("I am an enemy");
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public void getHealth() {
        System.out.println(health);
    }
}
public class Main {

    public static void main(String[] args) {
    // write your code here
       Enemy enemy = new Enemy();
       enemy.setHealth(30);
       enemy.getHealth();
    }
}

Those are the four pillars of OOP in Java.

Buy me a cup of coffee