From the Oracle's website:
"A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype of T. "
"Narrowing reference conversions: From any reference type S to any reference type T, provided that S is a proper supertype of T. "
1.) On base of these, with my own words, widening means: reference conversion from the child class to its parent class. Like this:
Parent father = new Child(); // if the Child class declared it extends the Parent class.
And the narrowing means reference conversion from the parent (super) class to its child class:
Child ch = (Child)father; // but it can be do only if the "father" variable is type of Parent, and it have to refers to an object of Child class.
Please correct me if i am wrong or confirm if i understood well!
2.) I don't understand why the conversion from child to its parent has the name "widening".
Parent class has fewer methods than child class, and it means more narrow possibilities.
(The child class extends the parent class!)
It would be more logical for me if this conversion would has the name "narrowing".
What kind of rational logic is in this naming?
Reference conversions: widening and narrowing
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
6 January 2021, 20:54
Probably has to do with how super classes can be used as a type for any of their sub classes:
In this code you can cast all of those to A, however if you were to try to cast the variable 'b' to an object of class C there would be an error. Moving down narrows the scope. This is a very important concept of polymorphism. Interfaces use this concept of upcasting as an interface doesn't have any actual code, it just defines methods that whatever class implements the interface has to have. You can have thousands and more different classes that implement the same exact interface and all you have to do is use a variable of that interface to be able to use any one of those classes:
In this code there is an interface with some methods, most likely for some sort of vehicle. Then in the solution class is a method that accepts an object of that interface and calls the methods. This interface could be implemented on a unicycle, bicycle, tricycle, submarine, airplane, car, truck, train, etc.... but the method doesn't care as long as whatever object passed to it implements that interface. It is very flexible. 0
Gellert Varga
6 January 2021, 23:17
Thank You, but it's a little bit difficult for me. The answer is saved, and my hope is it will get more clear for me later.
But one more question: in point 1. in my question was everything right?
0
Nouser
6 January 2021, 23:37
You look at it from the feature side of classes. While the Object class has just some elementary methods, other classes have lots more features.
But what about taking the type into account. You can cast nearly all references to the Object type, it can hold a wide range of subtypes. But to some interface type you can cast just members of that interface, the range of types an interface reference can handle is very narrow.
0
Gellert Varga
7 January 2021, 00:11useful
Thanks! I understand.
Into the child (Cat) class i can't put whatsoever type of object, only a narrow group of objects.
= narrowing.
(But let's leave the interfaces, I don't know anything about them yet...) +1