Subtyping and Java

Subtyping and Java

We will start this article with the below diagram.

Article content

The above diagram defines a type hierarchy with a type, called SuperType, at the top of the hierarchy. Another important point is that subtypes can have subtypes.

Now question is...

How do you define a type hierarchy in Java?

In Java, we define a type hierarchy using inheritance. Whenever inheritance comes into picture, we picturize a superclass-subclass relationship. Consider the below classes.

Article content

Here ClassT is the superclass and ClassV is the subclass. Now consider the below code snippet:

Article content

The above code snippet will generate below output:

Article content

The class of the object v is ClassV and the superclass of ClassV is the ClassT.

Now consider the below classes and interfaces.

Article content

You have written the below code snippet based on the above classes and interfaces.

Article content

The above code snippet will generate below output:

Article content

In this case, superclass of ClassV is Object class. Why? When a class doesn’t extend any other class, it implicitly extends java.lang.Object class.

We will have a look at another example. Consider the below classes and interfaces.

Article content

You have the below code snippet:

Article content

The above code snippet will generate below output:

Article content

The class of the object v is ClassV and the superclass of ClassV is the ClassT.

Now time to go one step further with the below program (we call it Program A).

Article content
Article content

If you run the above program, you will get the below output:

Article content

We can use the below diagram to represent the relationships between interface InterfaceA and classes ClassT and ClassV.

Article content

Here, objects of class ClassV may have three useful declared types: ClassV type, ClassT type, and InterfaceA type.

An object’s declared type determines the methods you can call on them.

An object’s actual type may be different than its declared type. Consider the below examples:

Article content

Compiler does the type checking based on the declared types. Declared types determine the legally allowed method calls. The below picture will help you understand better an object’s declared type and allowed method calls.

Article content

In this case, InterfaceA, ClassT, and Object are supertypes of subtype ClassV. In Java, we can define supertypes by both interfaces and classes.

In Java, every subclass is a Java subtype. Therefore, the subclass ClassV is a subtype.

In the above example, ClassT is the direct supertype of ClassV because ClassT is the direct superclass of ClassV (ClassV extends ClassT). Moreover, InterfaceA is the direct supertype of ClassV because InterfaceA is the direct super interface of ClassV (ClassV implements InterfaceA).

Article content

In the above example, ClassV is the subtype of ClassV (reflexive), ClassV is a subtype of ClassT, and ClassV is a subtype of InterfaceA.

That’s all for this article. In my next piece, you’ll learn about True Subtyping and more.

I look forward to your thoughts in the comments section!

Takahide Maruoka

Credly Top Legacy Badge Earner | ISO/IEC FDIS 42001 | ISO/IEC 27001:2022 | NVIDIA | Google | IBM | Cisco Systems | Generative AI | AWS

7mo

Thank you for info.

Like
Reply
will W.

--Transformational Speaker- Priest- Sports- Tech

7mo

This simple pulling back the curtain, is very useful as people will see the complicated can be simple when you look closer...

To view or add a comment, sign in

More articles by Sanjoy Kumar Malik .

Explore topics