[OOP 4] Encapsulation, Method Overloading and Method Overiding

[OOP 4] Encapsulation, Method Overloading and Method Overiding

-in Java โ˜•

ยท

5 min read

๐Ÿ“—Encapsulation in Java

๐Ÿ‘‹ First of all, let's start with a quick definition. In Java, encapsulation is the practice of hiding the internal details of an object from the outside world and providing access to it only through a public interface.

๐Ÿ”’ Think of encapsulation as putting your object's data inside a locked box or a capsule, so that no one can directly access or modify it from outside the object.

๐Ÿง Here's an example: let's say you have a BankAccount class that has a balance variable. You want to make sure that no one can access or modify the balance variable directly, but only through public methods.

class BankAccount {
    private double balance; // this variable is private

    public double getBalance() {
        return balance; // this method provides read-only access to balance
    }
    public void deposit(double amount) {
        balance += amount; // this method modifies the balance variable
    }
    public void withdraw(double amount) {
        balance -= amount; // this method modifies the balance variable
    }
}

๐Ÿ”‘ In this example, the balance variable is marked as private, which means that it can only be accessed or modified from within the BankAccount class.

๐Ÿ”“ However, the BankAccount class provides public methods like getBalance(), deposit(), and withdraw() to allow outside code to access and modify the balance variable indirectly.

๐Ÿ‘ And that's encapsulation in Java in a nutshell! By using encapsulation, you can protect your object's data and ensure that it is only accessed or modified in a controlled way through public methods.

๐Ÿ“˜Get and Set in Java

n Java, getters and setters are a pair of public methods that are used to access and modify the private variables of a class.

๐Ÿ‘€ Getters are public methods that provide read-only access to a private variable, while setters are public methods that allow outside code to modify the private variable.

๐Ÿฆ Let's use the BankAccount class as an example again. In the previous example, we made the balance variable private to prevent outside code from directly accessing or modifying it.

๐Ÿ‘จโ€๐Ÿ’ป To allow outside code to access and modify the balance variable, we can add a public getter and setter method for it:

csharpCopy codepublic class BankAccount {
    private double balance;

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }
}

๐Ÿ”‘ In this updated example, we've added a public getBalance() method that provides read-only access to the private balance variable.

๐Ÿ”“ We've also added a public setBalance() method that allows outside code to modify the private balance variable. Notice that the setBalance() method takes a parameter balance, which is used to set the value of the private balance variable using the this keyword.

๐Ÿ“—Method overloading

Method overloading in Java is when you have multiple methods in a class with the same name but different parameters. ๐Ÿง

For example, imagine you have a class called Calculator and you want to create a method that can add numbers. You could create a method called add that takes in two integers and returns their sum. ๐Ÿค”

But what if you also want to add decimal numbers or more than two numbers? This is where method overloading comes in! ๐Ÿคฉ

You can create additional "add" methods in the same class with different parameters, like one that takes in three integers, one that takes in two doubles, or one that takes in an array of integers. ๐Ÿคฏ

This allows you to use the same method name for different variations of the same functionality, making your code more flexible and easier to read. ๐Ÿ“š

๐ŸซกHere's an example,

class Calculator {
    static int add (int a, int b) {
        return a + b;
    }

    static double add (double a, double b) {
        return a + b;
    }
}

class Main {
    public static void main(String[] args) {
        System.out.print(Calculator.add(2, 4) + ", ");
        System.out.println(Calculator.add(5.5, 5.5));
    }
}

Output:

6, 11.0

So the next time you need to perform the same task with different types or numbers of parameters, remember to use method overloading in Java! ๐Ÿ‘

๐Ÿ“—Method Overriding

๐Ÿ‘‹In Java, method overriding is a way to provide a new implementation for a method that already exists in a superclass or parent class.

๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ For example, imagine a Class Parent that has a method called greet(). This method simply prints out Hello!.

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง Now, let's say we have a subclass called Child that extends the Parent class. If we want the "Child" class to have a different greeting, we can override the greet() method.

๐Ÿ‘ฉโ€๐Ÿ’ป Here's an example of how we could do that in Java code:

public class Parent {
    public void greet() {
        System.out.println("Hello! I am Parent");
    }
}

public class Child extends Parent {
    @Override
    public void greet() {
        System.out.println("Hello! I am Child");
    }
}

๐Ÿง In this example, the Child Class has overridden the greet() method from the Parent class. Now, if we create an instance of the Child class and call the greet() method, it will print out Hi there! instead of Hello!.

Thank You Soo Much for your valuable time.๐Ÿ˜Š๐Ÿฅณ๐Ÿ‘‹


๐Ÿ‘‹ Hi there! Let's connect and collaborate!

Here are some ways to reach me:

๐Ÿ”น GitHub: github.com/mithindev

๐Ÿ”น Twitter: twitter.com/MithinDev

๐Ÿ”น LinkedIn: linkedin.com/in/mithin-dev-a-397983247

Looking forward to connecting with you!

ย