๐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!