Introduction
Sometimes, one might think can we implement the friend concept in Java just like in C++ or is it not possible without the friend keyword to access private and protected members of the class and how to implement this friend function in Java?

We will find answers to all such questions in this article on the friend function in Java.
Read the topic about- Iteration Statements in Java, Duck Number in Java.
Friend Concept in Java
Java does not have the friend keyword like C++, which is used to access the non-public members of a class. Nevertheless, we can achieve this functionality.
Before we try to implement friend functionality, we need to see what it is precisely in C++ and why we need it, and what we can do using this keyword.
Friend Class
A friend class gets access to private and protected members of another class in which it is declared a friend. When granting access to a class, you need to specify that the access is granted to a class using the 'friend' keyword
friend class xclass;
In the given below example, the the_binary_tree class can access data directly
class Node {
private:
int data;
int key;
//...
friend class the_binary_tree;
};
Note - The friend declaration can go in a class's private, protected or public section. It does not matter where they appear.
Must Read Type Conversion in Java
Friend Function
In C++, a friend function is declared outside a class but can access non-public members of the class, that is, the private and protected members.
Sometimes while programming situation arises when we want two classes to share their members. These members may be the data members of the class, the class functions or function templates. In such cases, we make the friend function to both these classes, which will allow accessing private data members and protected data of members of the class.
Generally, non-member functions cannot access the private members of a particular class. Once declared as the friend function, that function can access these classes' private and protected members.
Now let us try to implement the friend function in Java.
Consider two persons from different departments of a company.
Both people don't know each other, but they need to cooperate for some work. Let us set one employee as Anna and the other as Jess based on a friend pattern.
We must create two packages and implement both classes to implement this example.
The class Anna in the department(package) codingninjas1
package codingninjas1;
import codingninjas2.Jess;
public final class Anna {
static {
/* Declare classes in the codingninjas2 package as 'friends.' */
Jess.setInstance(new Jess_Implement());
}
/* Constructor is only accessible by 'friend' classes. */
Anna() {
}
/* This method is only accessible by 'friend' classes.*/
void Hellocodingninjas() {
System.out.println("Hello! I am Anna from codingninjas.");
}
static final class Jess_Implement extends Jess {
protected Anna createAnna() {
return new Anna();
}
protected void sayHello(Anna anna) {
anna.Hellocodingninjas();
}
}
}
The class Jess in the department(package) codingninjas2
package codingninjas2;
import codingninjas1.Anna;
public abstract class Jess {
private static Jess instance;
static Jess getInstance() {
Jess a = instance;
if (a != null) {
return a;
}
return createInstance();
}
private static Jess createInstance() {
try {
Class.forName(Anna.class.getName(), true,
Anna.class.getClassLoader());
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
return instance;
}
public static void setInstance(Jess jess) {
if (instance != null) {
throw new IllegalStateException("Jess instance already set");
}
instance = jess;
}
protected abstract Anna createAnna();
protected abstract void sayHello(Anna anna);
}
The class to implement the main method
package codingninjas2;
import codingninjas1.Anna;
public final class Friend_Class {
public static void main(String[] args) {
Jess jess = Jess.getInstance();
Anna anna = jess.createAnna();
jess.sayHello(anna);
}
}
With two classes in different packages, the code above implements the friend class functionality in java. Class Jess acts as a friend class that accesses the class Anna members.
Output
Hello! I am Anna from codingninjas.
Before wrapping up, the article on friend function in java, let's see the difference between friend function and friend class.




