Class-Friendship via Interface

light111Class-Friendship via Interface


When one class wants to access the private method/attribute of another class, it is not generally possible but possible via a CLASS Friendship.

1

This is the usual class friendship that allows access to the private methods/attributes.

2

Output:

3


This friendship principle is possible via the interface also. The first class grants friendship to the interface and other class(es) if implement(s) this interface then it gets the permission to access the private methods/attributes. Below is the example:-

4

Output:-

5


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s