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 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. Using this way we provide 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 a 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 a 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’ for instance variables,…

…and calculate interest() method to calculate the 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 account class so it comes under the default access specifier now.

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 the 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 calculated interest method is very sensitive. 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 says interest and makes it a 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 private, an object to it can not be created in another class.

But here one question may arise in your mind 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.

Here we can write all possible user verification scripts inside the contact method.

So when a user calls the contact method and 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 the 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

You can also go through a few more blog links below related to core Java:

Stack vs Heap Memory Allocation In Hindi…
Inner Class In Java In Hindi…
Instance Variable vs Class Variable In Hindi…
Difference Between Instance Variable And Static Variable In Java…
Inner Class With Suitable Example In Java…
Difference Between PHP and JAVA In Hindi…
Why Java Is A Secure Language In Hindi…
Relational operator in Hindi in Java…
Logical operator in Hindi in Java…
Assignment Operator in Hindi in Java…
Unary operator in Java example in Hindi…
Arithmetic operators in Java in Hindi…
Operators In Java In Hindi…
Types Of Inheritance In Hindi…
Garbage Collection In Java In Hindi…
Interface In Java In Hindi|java में Interface क्या होता है…
Access Specifier In Java In Hindi…
Explain Types Of Jdbc Drivers In Detail…
Super Keyword In Java In Hindi…
Static Keyword in Java in Hindi…

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. To achieve 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 for the sensitive data declared in the class.

In the case of any queries, you can write to us at support@a5theory.com 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 feedback 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.