Java Class References

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 parent name
  3. Reference by name of 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 of X

class Y extends Thread

  {

    public void run()

      {

        System.out.println("thread is start running......");

      }

    //Assume Y will complete there work in 5 days.

  }

X.java

//Y class of X

class X extends Y

  {

    public static void main(String[] args)

      {

        X a=new X();

        a.start();

      }

  }

//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

//Y class of X

class X extends Y

  {

    public static void main(String[] args)

      {

        X a=new X();

        a.print();

      }

  }

//Assume X class complete there work in 5 minutes.

Printable.java

abstract class Printable

{

abstract void print();

//this type of concrete methods are still allowed in abstract classes

void printme()

{

System.out.println("Invoke A");

}

}

Y.java

//class of X

class Y extends Printable

  {

    void print()

      {

        System.out.println("Y implements Print method");

      }

    //Assume Y will complete there work in 5 days.

  }

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

//Y class of X

class X extends Y

  {

    public static void main(String[] args)

      {

        X a=new X();

        a.print();

      }

  }

//Assume X class complete there work in 5 minutes.

Printable.java

public interface Printable

  {

    public void print();

  }

Y.java

//class of X

class Y implements Printable

  {

    public void print()

      {

        System.out.println("Y implements Print method");

      }

    //Assume Y will complete there work in 5 days.

  }

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 refered 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 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 only method a 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 of A

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 our 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

Fig-4.jpg

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

fig-5.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all