Generalization and Specialization in Java

27 Jun, 2022
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.
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

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.
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:
Java
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 line to see the error        // father.play();    }}
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
Java
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();    }}
Earning Son

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 Narrowing is not allowed
Java
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);        }    }}
java.lang.ClassCastException: Father cannot be cast to Son

Example:
Java
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    }}
Earning Father
Enjoying son

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.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.
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.
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.
kalrap615
ManasChhabra2
vinayedula
java-advanced
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Queue Interface In Java
Multidimensional Arrays in Java
How to add an element to an Array in Java?
Stack Class in Java
PriorityQueue in Java
Math pow() method in Java with Example
Initialize an ArrayList in Java
List Interface in Java with Examples
ArrayList in Java