Introduction
In this article, we discuss how one class call another class in Java.
Java Class Reference (a class referring to another)
There are three ways to refer to Java classes, they are:
- Reference by name
- Reference by the parent name
- Reference by name of the interface (role).
1. Reference By Name
In this, a class can call another class directly using the other class's name.
Let's take an example
In this example, we assume that Y can complete the work in 5 days and X can complete the work in 5 minutes.
Y.java
- class Y extends Thread
- {
- public void run()
- {
- System.out.println("thread is start running......");
- }
- }
X.java
-
-
- class X extends Y
- {
- public static void main(String[] args)
- {
- X a=new X();
- a.start();
- }
- }
-
-
Output
Advantages
- In this case, the main benefit is that we can't make a different interface or class to implement all the objects of another class. So by using reference by name we can implement all the methods of another class directly.
Limitations
- X needs to wait 5 days to compile the task, in other words only those classes can be directly referred to that are known and available. This limitation of referring to a known and available class is called "static classing environment".
- If Y changes the name of his class or a referenced method then X needs to modify his class, in other words, Reference by name creates a tight coupling between referencing and referenced classes.
2. Reference By Name Of Parent Class
Example
In this example; we take a parent class (in other words Printable. Java class). The purpose of this class is to refer to any unknown and unavailable classes in the future. But one condition should exist that these classes must be a part of the referenced family.
X.java
-
-
- class X extends Y
- {
- public static void main(String[] args)
- {
- X a=new X();
- a.print();
- }
- }
-
-
Printable.java
- abstract class Printable
- {
- abstract void print();
- void printme()
- {
- System.out.println("Invoke A");
- }
- }
Y.java
- class Y extends Printable
- {
- void print()
- {
- System.out.println("Y implements Print method");
- }
- }
Output
Advantages
- Unknown and available classes can be referred to as long as they are a part of the referenced family.
- This facility of referencing unknown and unavailable classes is called a dynamic classing environment.
- Reference by parent family facilitates a dynamic classing environment within a family.
Limitations
- The parent class family needs to be known and available.
- The referencing class is tightly coupled with the parent family.
3. Reference By Name Of Interface (Role)
X.java
- class X extends Y
- {
- public static void main(String[] args)
- {
- X a = new X();
- a.print();
- }
- }
Printable.java
- public interface Printable
- {
- public void print();
- }
Y.java
- class Y implements Printable
- {
- public void print()
- {
- System.out.println("Y implements Print method");
- }
- }
Output
Advantages
- No class family is needed to be known and available, in other words, only the class belonging to a family can be referred to as long as it plays the referenced role.
- Referencing and referenced classes are loosely coupled.
We have now seen many terms, like class, abstract class (we called family) and interface (role) in our Java class reference topic. Now for an example, that shows the difference between them.
Example
The following example demonstrates the need for a role-based inheritance, in other words, the need of only a name.
Scenario
Let there be three programmers, named X, Y and Z. X defines a class named ABC that contains a, b and c methods. X needs to expose the only method of his class to the class of Y and only b method to the class of Z. X can do this as in the following.
First, we define class X
ABC.java
- class ABC implements A, B
- {
- public void a()
- {
- System.out.println("Method a");
- }
- public void b()
- {
- System.out.println("Method b"):
- }
- public void c()
- {
- System.out.println("Method c");
- }
- }
In this case, we have three types of references as we discussed previously. Let's take all one by one.
1. Reference By Name
By the scenario that we have taken, this approach fails as if we referred to the class directly, in other words, we implement all the methods of class X automatically but we don't need all of them. So we can't use this approach.
2. Reference By Family Name
By referencing family name it creates a problem since we can't extend two abstract classes simultaneously or one of the reasons of not using this approach is that when we are using a family reference we implement both a role and features but in this scenario, we need only a role (in other words signature). So this approach also fails.
3. Reference By Role (Interface)
This approach is better and perfect for our scenario. By using this case we only show one method through this and can construct any number of interface and implementations in a class. Look at the following.
Create the following classes for performing the given function
Now we need to create the following classes to perform the given function.
For the given scenario, first, we create various interfaces (in other words interface P and Q) for invoking only a single method that the class needs.
For class Y we create an interface A that contains only A methods and for class Y we need to create an interface B that contains only method b.
class of Y
A.java
- public interface A
- {
- public void a();
- }
AInheriter.java
- class AInheriter implements A
- {
- public void a()
- {
- System.out.println("invoke method a");
- }
- public static void main(String args[])
- {
- AInheriter abc = new AInheriter();
- abc.a();
- }
- }
Output
class of Z
B.java
- public interface B
- {
- public void b();
- }
BInheriter.java
- class BInheriter implements B
- {
- public void b()
- {
- System.out.println("invoke method b");
- }
- public static void main(String args[])
- {
- BInheriter abc = new BInheriter();
- abc.b();
- }
- }
Output