|
In case of object references, the type become wider as you move "up" in a class hierarchy. This generic rule is the base of all the specific rules in reference conversion. Figure 10.5 shows an inheritance hierarchy of Account class. This class implements an interface Auditable. It has two subclasses CheckingAccount and SavingsAccount.
![]() |
Let us discuss the rules for (object) reference conversion with the help of this example hierarchy.
1. A class-type can be converted to another class-type, if the new class-type is its superclass.
A class is considered as a wider type than its subclasses. Hence, widening conversion occurs when you change the type of any of the subclasses into their superclass. In the following code, checkingAcc is class-type object reference.
CheckingAccount checkingAcc = new CheckingAccount();
When you store the checkingAcc into a variable of Account type, widening conversion occurs as:
Account acc = checkingAcc; // valid! Widening reference conversion
The narrower reference checkingAcc is stored in wider reference acc. Java will implicitly convert the type of checkingAcc from CheckingAccount to Account type. This type conversion takes place because Account is a superclass of CheckingAccount. Figure 10.6 shows the widening reference conversion with planter metaphor. The wider planter represents the wider type.
![]() |
Figure 10.6 Widening conversion for object reference from one class-type to another class-type
As you can see that, the wider planter can accommodate the smaller planter. Similarly, the reference variable of superclass can accommodate reference to any of its subclasses.
|
|
Since Object class is a superclass of all Java classes, object reference of any type can be stored into a reference variable of java.lang.Object type. |
On the other hand, the subclass references cannot hold the reference of the superclass. Any such attempt results in error. For example, the following code will not compile:
Account acc = new Account();
CheckingAccount checkingAcc = acc; // Compile-time error!
acc is reference to Account object. You cannot store it in checkingAcc as it is a narrower type. The compiler will throw an error saying something like “Type mismatch: cannot convert from Account to CheckingAccount”
|
|
Java does not allow the automatic conversion to a subclass type for a good reason. Such conversion could result in runtime exception. For example, if Java had allowed such conversion, the checkingAcc will refer to an Account object. Therefore, you can write a code that calls methods in CheckingAccount class on checkingAcc. But in reality, the checkingAcc would be referring to an Account object. Calling the methods of CheckingAccount class on it is erroneous. |
2. A class-type can be converted to an interface-type, if the class implements that interface.
When a java class implements any interface, that interface-type is considered as wider than the class-type. Hence, when you change a implementing class’s type into an interface-type, it is a widening conversion. In the following code, acc is a class-type object reference.
Account acc = new Account();
Since Auditable is an interface that Account implements, you can assign acc to a variable of Auditable interface-type. Following code assigns acc to the reference variable auditable.
Auditable auditable = acc; // valid! Widening reference conversion
acc is of narrower type than the reference variable auditable. Java will implicitly convert the type of acc from Account to Auditable type. Figure 10.7 shows the widening reference conversion with planter metaphor. The wider planter represents the interface-type that is wider than the implementing class type.

Figure 10.7 Widening conversion for object reference from a class-type to an interface-type
As you can see that, the wider planter accommodates the smaller planter, the reference variable of interface-type can accommodate reference to any of its implementing classes. Since all the subclasses of Account are indirectly implementing the Auditable interface, their type can fit in the interface-type Auditable. Following code shows all valid assignments:
CheckingAccount checkingAcc = new CheckingAccount();
SavingsAccount savingsAcc = new SavingsAccount();
Auditable auditable1 = checkingAcc; // valid
Auditable auditable2 = savingsAcc; // valid
You can thus assign the references of CheckingAccount and SavingsAccount to the interface-type Auditable. These two classes are indirect implementers of Auditable interface.
Java forbids the narrowing of datatypes. As the smaller planter cannot hold the wider planter, the implementing class references cannot hold the reference of the interface. Any such attempt results in error. For example, the following code will not compile:
Account acc1 = new Account();
Auditable auditable = acc1; // valid
Account acc2 = auditable; // Compile-time error!
auditable is reference of interface-type Auditable. You cannot store it in acc2 as its type is a narrower type. The compiler will throw an error saying something like “Type mismatch: cannot convert from Auditable to Account”
3. An interface-type can be converted to new interface-type, if it extends the new interface-type.
An interface-type is considered as narrower than its super interface. A super interface of an interface is the interface it extends. For example, in the class hierarchy we have been discussing so far, assume that interface Auditable extends from another interface Reviewable as:
interface Reviewable {}
interface Auditable extends Reviewable {}
Now Reviewable is a super interface of Auditable and hence it is of wider type than Auditable. You can assign type Auditable to the type Reviewable. In the following code, interface-type object reference auditableAcc is assigned to a variable of its super interface type reviewableAcc.
Account acc = new Account();
Auditable auditableAcc = acc;
Reviewable reviewableAcc = auditableAcc; // valid!
You can assign auditableAcc to a variable of Reviewable type as it will be a widening conversion. The auditableAcc is of a narrower type than reviewableAcc. Java will implicitly convert the type of auditableAcc from Auditable to Reviewable type. Figure 10.8 shows the widening reference conversion for interface-types with planter metaphor. The super interface is represented with a wider planter as it can hold its sub interface type.
![]() |
Figure 10.8 Widening conversion for object reference from one interface-type to another interface-type
As you can see, the interface-type can fit into the its super interface type. Note that an interface can have multiple super interfaces. In that case, it can be assigned to any of its super interface types.