Inner Class With Suitable Example In Java.

Hello, friends in this blog post(Inner Class With Suitable Example In Java) we are going to discuss the inner class in java. As it is almost clear from the name that inner class is a class that is written inside another class.

An inner class is basically a safety mechanism since it is hidden from other classes in its outer class.|Inner Class With Suitable Example In Java|

To make instance variables not available outside the class, we use a private access specifier before the variables. And using this way we provide the security to the variable.|Inner Class With Suitable Example In Java|

Similarly, in some cases, we need to provide security to the entire class. And for doing this we can use private access specifier before the class.

But here the problem is that if we make any class private then it will not be available to the JVM or java compiler. So it will be an illegal activity to use private access specifier before a class in java.

But private specifier is allowed with the inner class and this can provide security to the entire inner class. once we inner class is declared private then it will not be available to the other class.

We can also say that the object of the inner class can not be created in any other class. Now we try to make you understand this concept with the help of an example so that you can better understand this.

Suppose we are writing BankAcct class with bank account details like ‘Balance’ and ‘rate of interest’ as for instance variables, and calculate interest() method to calculate interest amount and add it to the balance amount, as shown below:

class BankAcct

{

//Balance and rate of interest

Private double bal;

private double rate;

//calculate interest and update balance

void calculateinterest()

{

double interest = bal*rate/100;
bal += interest;
system.out.println("Balance="+bal);

}
}

So here we can see that there is no security to the code as we did not use any access specifier before the bank acct class so it comes under the default access specifier now.

And as per the default specifier, this class is available to any other classes outside. So any other class can easily create the object of this class and can access the members of this class. Hence there is no security for BankAcct class.

For example, a programmer can write another class, where he can create the object to the BankAcct class and can call the CalculateInterest method as:

class Myclass
{
public static void main(String args[])
{
BankAcct account =  new BankAcct(10000);
account.calculateinterest(9.5);
}
}

The calculate interest method is a very sensitive method. It should be protected from outsiders. If it is available to the other programmer, they may call it as shown earlier above and the balance amount in the bank will be updated.

But in the bank only authorized people should be able to update the balance amount. hence the CalculateInterest method should not be available to others.

So to provide the security to calculate interest method and rate of interest we have to write them inside another class say interest and make it private inner class inside the BankAcct class as shown below.

Private class Interest
{
Private Double rate;
Void calculate interest()
{
double interest = bal*rate/100;
bal += interest;
system.out.println("balance="+bal);
}
}

Now, since the interest class is declared as private, an object to it can not be created in another class. But here one question may arise in your mind that how can we use this class if the object to it can not be created?

So the answer is that we can create an object of the inner class in its outer class. For this purpose, we can write a method contact() in the outer class where the object of the inner class is to be created.

So any programmer who wants to access the inner class will use the contact method, and here we can write all possible user verification scripts inside the contact method.

So when a user calls the contact method and if it is verified successfully then only the object of the inner class will be created. And then the only user will be able to access the inner class. You can see the design of the contact method below:

//In this method, inner class object is created after validation.
//The authenticity of the user. r is the rate of interest.

Void contact(double r) throws IOException
{
//access the password from keyboard and verify 
Bufferreader br = New Bufferreader(New Inputstreamreader(system.in));
system.out.print('Enter password :');
String passwd = br.readLine();
if(passwd.equals("xyz123"))
{
// if password is correct then calculate interest
interest in = New interest(r);
}
else{
system.out.println("Sorry, you are not a authorized person");
}
}

Now, see the complete program to create an inner class Interest in the outer class BankAcct. Also, in this program, we are showing that a user can use the inner class by classing the contact() method of the outer class, where authentication is checked.

Thus, the outer class is acting as a firewall (Security mechanism) between the user and inner class.

//Let us make a program to create the outer class BankAcct and the inner class Interest in it:

//Inner calss example
//this is outer class

import java.io.*;

class BankAcct
{
//balance amount is the variable
private double balance bal;
//initialize the balance
BankAcct(double b)
{
bal = b;
}
//in this method, inner class object is created after verifying
//the authentication of the user, r is the rate of interest.
//this method accept the rate of interest rate

void contact(double r) throws IOException
{
//accept the password from the keyboard and verifying
Bufferreader br = New Bufferreader(New Inputstreamreader(system.in));
system.out.print('Enter password :');
String passwd = br.readLine();
if(passwd.equals("xyz123"))
{
// if password is correct then calculate interest
interest in = New interest(r);
}
else{
system.out.println("Sorry, you are not a authorized person");
}
}
//inner class
private class Interest
{
//rate of interest
private double rate;
//initialize the rate
Interest(double r)
{
rate = r;
}
//calculate interest amount and update the balance
void calculate interest()
{
double interest = bal*rate/100;
bal += interest;
system.out.println("updated balance = "+bal);
}
}
}
//using inner class
Class innerClass
{
public static void main(string args[]) throws IOException
{
//bank account holding a balance of 10000;
BankAcct account = New BankAcct(10000);
//update balance amount by adding interest at 9.5%
account.contact(9.5);
}
}

Output:

C:\> javac Innerclass.java
C:\> java Innerclass
Enter password xyz123
updated balance = 10950.0

Conclusion:

So friends, within this blog post(Inner Class With Suitable Example In Java) we have learned about the inner class in java. When we simply declare any class with a private access specifier then we can not create the object of that class in any other class outside but this is considered an illegal activity in the programming. For achieving this we create an inner class and then we can declare that class private and can access its members by creating its object in the outer class with proper authentication of the user. This way we can achieve security to the sensitive data declared into the class.

In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.

Hope! you would have enjoyed this post |Inner Class With Suitable Example In Java|.

Please feel free to give your important feedbacks in the comment section below.

Have a great time!

Anurag

I am a blogger by passion, a software engineer by profession, a singer by consideration and rest of things that I do is for my destination.