Class Casting in Java | Generalization, Specialization in Java

The process of converting a class type into another class type having relationship between them through inheritance is called class casting in java

We have known in the previous tutorial that a class is a referenced data type. So, if classes have some relationship between them through inheritance, it is also possible to convert a class type into another class type through casting. 

For example, we can not convert a Dog class into Horse class if there is no relationship between them through inheritance. Let’s look at the below figure which shows the classes Department, College, and University have a relationship among them through inheritance. 

Class casting in Java

From the above figure, since a College class is derived from University class through inheritance, we can convert a College class into a University class through casting. Similarly, we can also convert a Department class into College class, since Department is subclass of College class. 

Generalization and Specialization in Java


The process of converting sub class type into super class type is called generalization because we are making the subclass to become more general so that its scope can be more widening. This conversion is also called widening or upcasting in java. Let’s understand generalization by taking a realtime example.

Let us take a super class Mobile and sub classes are Samsung and Samsung Galaxy as shown in the below figure.

Generalization and Specialization in Java

When we talk about a mobile, In general, it may represent any kind of mobile. So, here, the scope is widened. Now, suppose we talk about Samsung mobile, then we come down one step in the hierarchy of inheritance and we have eliminated any other kind of mobiles. 

Thus, we are becoming more specific. When we still come down to Samsung Galaxy, we are pointing only Samsung Galaxy mobile and not any other Samsung mobile. Thus, this is very specific. This means that when we move down from super class to sub classes, we are becoming more and more specific. 

When we go back from sub classes to super class, we are becoming more general. This is called generalization in java. Generalization is safe because classes will be more general. Hence, we do not need a cast operator to perform generalization. Java compiler will do the implicit casting in generalization. 

The conversion of a super class type into sub class type is called specialization in java. Specialization means going down from a more general form to a more specific form. Thus, its scope will be narrowed. Hence, this conversion is also called narrowing or down-casting in java

Specialization is not safe because classes will be more and more specific. In this case, we will need cast operator. Java compiler will do explicit casting in the specialization.

Final words
Hope that this tutorial has covered almost all important points related to class casting, generalization, and specialization in java. I hope that you will have understood the basic idea of these topics and enjoyed them.

If this tutorial is useful, please, share it on the social networking site for your friends.
Thanks for reading!!!
Next ➤ Upcasting and Downcasting in Java⏪ PrevNext ⏩

Leave a Comment