Generalization and Specialization in Java

General class?

Loosely speaking, a class which tells the main features but not the specific details. The classes situated at the top of the inheritance hierarchy can be said as General.



Specific class?

A class which is very particular and states the specific details. The classes situated at the bottom of the inheritance hierarchy can be said as Specific.

Example 1:



Relatively General Class: Money
Relatively Specific Class: Dollar, Euro, Rupees

Example 2:



Lemon, Orange are more Specific than Citrus
Banana, Apple are more Specific than Non-Citrus
Citrus, Non-Citrus are more Specific than Fruit
Fruit is most general class

Conversion of one class type to another class type?

We can convert references to one class type to another class type in Java. But for the conversion to happen the classes should be related with each other by the way of inheritance.

Therefore,

  • References for Vehicle and Bus can be type-casted to each other.
  • References for Vehicle and Car can be type-casted to each other.
  • References for Vehicle and Truck can be type-casted to each other.
  • References for Bus, Car and Truck can’t be type-casted to each other.

Generalization

Converting a subclass type into a superclass type is called ‘Generalization‘ because we are making the subclass to become more general and its scope is widening. This is also called widening or up casting. Widening is safe because the classes will become more general.

For example, if we say Car is a Vehicle, there will be no objection. Thus Java compiler will not ask for cast operator in generalization.


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Father {
    public void work()
    {
        System.out.println("Earning Father");
    }
}
  
class Son extends Father {
    public void play()
    {
        System.out.println("Enjoying son");
    }
}
  
class Main {
    public static void main(String[] args)
    {
        // father is a superclass reference
        Father father;
  
        // new operator returns a subclass reference
        father = (Father) new Son();
  
        // which is widened using casting
        // and stored in father variable
        // Though casting is done but it is not needed
        father.work();
  
        // Uncomment next lone to see the error
        // father.play();
    }
}

chevron_right


Output:

Earning Father

So, in widening or Generalization, we can access all the superclass methods, but not the subclass methods.

Example: Now Suppose we override the superclass methods in sub class

filter_none

edit
close

play_arrow

link
brightness_4
code

class Father {
    public void work()
    {
        System.out.println("Earning Father");
    }
}
  
class Son extends Father {
    @Override
    public void work()
    {
        System.out.println("Earning Son");
    }
}
  
class Main {
    public static void main(String[] args)
    {
  
        // father is the super class reference
        Father father;
  
        // new operator returns a subclass reference
        father = (Father) new Son();
  
        // which is widened using casting
        // and stored in father variable
        // Though casting is done but it is not needed
  
        // subclass method is invoked
        father.work();
    }
}

chevron_right


Output:

Earning Son

Specialization

Converting a super class type into a sub class type is called ‘Specialization‘. Here, we are coming down from more general form to a specific form and hence the scope is narrowed. Hence, this is called narrowing or down-casting.

Narrowing is not safe because the classes will become more and more specific thus giving rise to more and more doubts. For example if we say Vehicle is a Car we need a proof.Thus, In this case, Java compiler specifically asks for the casting. This is called explicit casting.

Example: To show when Narraowing is not allowed

filter_none

edit
close

play_arrow

link
brightness_4
code

class Father {
    public void work()
    {
        System.out.println("Earning Father");
    }
}
  
class Son extends Father {
    public void play()
    {
        System.out.println("Enjoying son");
    }
}
  
class Main {
    public static void main(String[] args)
    {
  
        try {
            // son is a sub class reference
            Son son;
  
            // new operator returns a superclass reference
            // which is narrowed using casting
            // and stored in son variable
  
            // This will throw exception
            son = (Son) new Father();
  
            // Through a narrowed reference of the superclass
            // we can neither access superclass method
            // and nor the subclass methods
  
            // Below lines will show
            // an error when uncommented
            // son.work();
            // son.play();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.ClassCastException: Father cannot be cast to Son

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Father {
    public void work()
    {
        System.out.println("Earning Father");
    }
}
  
class Son extends Father {
    public void play()
    {
        System.out.println("Enjoying son");
    }
}
  
class Main {
    public static void main(String[] args)
    {
        // son is a subclass reference
        Father father;
  
        // new operator returns a subclass reference
        // which is stored in the father variable
        // father stores a Father class reference
        // because of implicit casting
        father = new Son();
  
        // father is narrowed
        Son son = (Son)father;
  
        son.work(); // works well
        son.play(); // works well
    }
}

chevron_right


Output:

Earning Father
Enjoying son

Conclusion:

  1. When a superclass reference (referring to superclass object) is narrowed, then using that reference we can access neither methods of subclass nor methods of superclass.
  2. When a subclass reference (referring to subclass object) is widened and then again narrowed, then using that reference we can access all the methods of the subclass as well as the superclass. This is the same as simple base class reference referring to base class object where superclass methods have got inherited.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.