Generalization and Specialization in Java
Last Updated :
27 Jun, 2022
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 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();
}
}
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();
}
}
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 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);
}
}
}
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)
{
// 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
}
}
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.
Similar Reads
OOPS | Generalization as extension and restriction using Java Generalization is the process of taking out common properties and functionalities from two or more classes and combining them together into another class which acts as the parent class of those classes or what we may say the generalized class of those specialized classes. All the subclasses are a ty
4 min read
Java Collections Interview Questions and Answers Java Collection Framework was introduced in JDK 1.2 which contains all the collection classes and interfaces. Java Collection is a framework that provides a mechanism to store and manipulate the collection of objects. It allows developers to access prepackaged data structures and algorithms for mani
15+ min read
Private and Final Methods in Java Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purp
5 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Abstraction by Parameterization and Specification in Java Abstraction by Parameterization and specification both are important methods in Java. We do this in hope of simplifying our analysis by separating attributes and details implementation for user requirements by showing the essential part to the user and hiding certain details for various purposes lik
4 min read
Generic Class in Java Java Generics was introduced to deal with type-safe objects. It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types. Generics also provide compile-time type safety which allows programmers to c
4 min read