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:
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 father;
father = (Father) new Son();
father.work();
}
}
|
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 father;
father = (Father) new Son();
father.work();
}
}
|
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 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 son;
son = (Son) new Father();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
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)
{
Father father;
father = new Son();
Son son = (Son)father;
son.work();
son.play();
}
}
|
Output:
Earning Father
Enjoying son
Conclusion:
- 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.
Last Updated :
27 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...