Class Casting in Java | Generalization, Specialization

The process of converting a class type into another class type having the 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. If classes have some relationship between them through inheritance, it is also possible to convert a class type into another class type through type casting.

For example, we can not convert a Dog class into Horse class if there is no relationship between them through inheritance.

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 type casting.

Similarly, we can also convert a Department class into College class, since Department is subclass of College class. These are the examples of class casting in java.

Generalization in Java with Example Program


The process of converting subclass type into superclass type is called generalization in java. This is 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 referenced data types. Let’s understand generalization by taking a realtime example.

Let us consider a superclass Mobile and subclasses 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 superclass to subclasses, we are becoming more and more specific.

When we go back from subclasses to superclass, 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.

Let’s take an example program to understand the widening effect where we will write superclass reference to refer to subclass object. Look at the source code to understand better.

Program source code 1:

public class A {
 void m1(){
   System.out.println("Superclass method");	 
 }
}
public class B extends A{
 void m2(){
   System.out.println("Subclass method");	 
 }
}
public class WideningTest {
public static void main(String[] args) 
{
 A a; // a is superclass reference variable.
  a = (A)new B(); // generalization (widening) because a is referring to subclass object. 
  a.m1();
  }
}
Output:
      Superclass method

Explanation:

1. In this example program, we have used superclass reference to refer to a subclass object. So, subclass object type will be converted into superclass type, as:

a = (A)new B();

2. Now you can observe that we have called the m1() method of superclass but we are unable to call m2() method of subclass. This is because if we call m2() method as a.m2();, java compiler would generate an error message during compilation time.


So, in generalization, we can access all the methods of superclass but not subclass methods.

Suppose, we override superclass method into subclass as shown in the below program 2, it is possible to access subclass method but not superclass method. Anyhow, we will get only 50% functionality of the program.

Program source code 2:

public class A {
 void m1(){
   System.out.println("Superclass method");	 
 }
}
public class B extends A{
 void m1() // Overriding superclass method.
 {
   System.out.println("Subclass method");	 
 }
}
public class WideningTest {
public static void main(String[] args) 
{
 A a; // a is superclass reference variable.
  a = (A)new B(); // generalization (widening) because a is referring to subclass object. 
  a.m1();
 }
}
Output:
       Subclass method

Specialization in Java with Example Program


The conversion of a superclass type into subclass 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 referenced data types.

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.

Let’s take an example program to see the narrowing effect by taking subclass reference to refer to superclass object.

Program source code 3:

public class A {
 void m1(){
   System.out.println("Superclass method");	 
 }
}
public class B extends A{
 void m2(){
   System.out.println("Subclass method");	 
 }
}
public class NarrowingTest {
public static void main(String[] args) 
{
 B b; // b is subclass reference variable.
  b = (B)new A(); // specialization (narrowing) because b is referring to superclass object. 
  a.m1();
  }
}
Output:
       Exception in thread "main" java.lang.ClassCastException: 
         inheritance.A cannot be cast to inheritance.B

Explanation:

As you observe in the above program, method call b.m1() is not executing the super class method.

Similarly, if we call subclass method m2() as b.m2(); it will give the same error message.

Hence, we cannot access any of the methods of the superclass or subclass in the specialization. We will get 0% functionality in this case.

The solution to this problem is available. We will not create an object of superclass as we did in the previous case.

This time, we will create a reference of superclass to refer to subclass object and will use narrowing (specialization) concept. Look at the below source code.

Program source code 4:

public class A {
 void m1(){
   System.out.println("Superclass method");	 
 }
}
public class B extends A{
 void m2(){
   System.out.println("Subclass method");	 
 }
}
public class CastingTest {
public static void main(String[] args) 
{
 A a; // a is superclass reference variable.
  a = new B(); // Superclass reference refers to subclass object.
  B b = (B)a; // Narrowing because we are converting class A's reference type as class B's type.
  b.m1();
  b.m2();
  }
}
Output:
       Superclass method
       Subclass method

Key Points:

1. If superclass reference refers to subclass object, all the methods of superclass is accessible but not subclass method.

2. If subclass reference refers to subclass object, all the methods of superclass and subclass are accessible because subclass objects avails a copy of superclass.

3. Generalization (widening) is performed by using subclass object, only methods of superclass are accessible. If we override methods of superclass into subclass, only subclass methods are accessible.

4. If specialization (narrowing) is performed by using superclass object, none of the superclass or subclass methods are accessible. It is useless.

5. If narrowing is performed by using subclass object, all the methods of superclass and subclasses can be accessed.


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.
Thanks for reading!!!

Next ⇒ Upcasting and Downcasting in Java⇐ PrevNext ⇒

Leave a Comment

x