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.
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.
- 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.
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
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
java.lang.ClassCastException: Father cannot be cast to Son
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.
- OOPS | Generalization as extension and restriction using Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.reverse() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.