Java Class References

In this article we discuss how one class call to another class in java.

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:
  1. Reference by name
  2. Reference by the parent name
  3. 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
  1. class Y extends Thread   
  2. {  
  3.  public void run()   
  4.  {  
  5.   System.out.println("thread is start running......");  
  6.  }  
  7. }  
X.java
  1. //Y class of X  
  2.   
  3. class X extends Y  
  4. {  
  5.     public static void main(String[] args)  
  6.       {  
  7.         X a=new X();  
  8.         a.start();  
  9.       }  
  10.  }  
  11.   
  12. //Assume X will complete there work in 5 minutes.    
Output
 
Fig-1.jpg
 
Advantages
  1. 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
  1. 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".
  2. 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
  1. //Y class of X  
  2.   
  3. class X extends Y  
  4. {  
  5.     public static void main(String[] args)  
  6.       {  
  7.         X a=new X();  
  8.         a.print();  
  9.       }  
  10. }  
  11.   
  12. //Assume X class complete there work in 5 minutes.   
Printable.java
  1. abstract class Printable   
  2. {  
  3.  abstract void print();  
  4.  void printme()   
  5.  {  
  6.   System.out.println("Invoke A");  
  7.  }  
  8. }    
Y.java
  1. class Y extends Printable   
  2. {  
  3.  void print()   
  4.  {  
  5.   System.out.println("Y implements Print method");  
  6.  }  
  7. }    
Output
 
Fig-2.jpg
 
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
  1. The parent class family needs to be known and available.
  2. The referencing class is tightly coupled with the parent family.

3. Reference By Name Of Interface (Role)

 
X.java
  1. class X extends Y   
  2. {  
  3.  public static void main(String[] args)   
  4.  {  
  5.   X a = new X();  
  6.   a.print();  
  7.  }  
  8. }   
Printable.java
  1. public interface Printable  
  2. {  
  3.     public void print();  
  4. }    
Y.java
  1. class Y implements Printable   
  2. {  
  3.  public void print()   
  4.  {  
  5.   System.out.println("Y implements Print method");  
  6.  }  
  7. }    
Output
 
Fig-3.jpg
 
Advantages
  1. 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.
  2. 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
  1. class ABC implements A, B   
  2. {  
  3.  public void a()   
  4.  {  
  5.   System.out.println("Method a");  
  6.  }  
  7.  public void b()   
  8.  {  
  9.   System.out.println("Method b"):  
  10.  }  
  11.  public void c()   
  12.  {  
  13.   System.out.println("Method c");  
  14.  }  
  15. }   
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
  1. public interface A  
  2.  {  
  3.     public void a();  
  4.  }    
AInheriter.java
  1. class AInheriter implements A   
  2. {  
  3.  public void a()   
  4.  {  
  5.   System.out.println("invoke method a");  
  6.  }  
  7.  public static void main(String args[])   
  8.  {  
  9.   AInheriter abc = new AInheriter();  
  10.   abc.a();  
  11.  }  
  12. }   
Output
 
Fig-4.jpg
 
class of Z
 
B.java
  1. public interface B  
  2. {  
  3.     public void b();  
  4. }    
BInheriter.java
  1. class BInheriter implements B   
  2. {  
  3.  public void b()   
  4.  {  
  5.   System.out.println("invoke method b");  
  6.  }  
  7.  public static void main(String args[])   
  8.  {  
  9.   BInheriter abc = new BInheriter();  
  10.   abc.b();  
  11.  }  
  12. }    
Output
 
fig-5.jpg