+ 3

Polymorphism question

I'm reading head first java, in the chapter 8 gives an example, which is: 1)Snowboard s = new Snowboard(); 2)Object o = s; And says: the key point is that even if the object is of type Snowboard, an Object reference to the Snowboard object can't see the Snowboard-specific methods. My question is: why with Object o = s, I can't use the Snowboard methods? If I assigned the reference variable o with s and Snowboard class inherits methods from Object

14th Jul 2019, 4:16 PM
Shadow Shadow
Shadow Shadow - avatar
9 Answers
+ 7
D_Stark I think this is what you mean? Parent parent = new Child(); You will only have access to the parent methods except the child implements an abstract method of the parent class (polymorphism). That would be the only design I'd recommend tbh ;) My answer leaves the default methods beside which were introduced with Java 8...
14th Jul 2019, 6:18 PM
Tashi N
Tashi N - avatar
+ 4
I'm trying to explain using metaphors... Object is the mother of all classes. Snowboard is a child of the Object class. The child (class) knows how to start a computer. Now you call the mother (class) and ask her to start the computer. But she can't. She doesn't even know that her child knows how to do it. Sorry, couldn't think of a better example right now. But I hope it helps.
14th Jul 2019, 5:30 PM
Tashi N
Tashi N - avatar
+ 4
Tashi N nice 👍
14th Jul 2019, 5:33 PM
D_Stark
D_Stark - avatar
+ 3
What I have gathered is every object you create is a sub class of class Object and therefore inherits all its methods so any class can also be of type Object but only its inherited and overridden methods will work all else in this Snowboard class is hidden. I think this is right it's still something I'm learning
14th Jul 2019, 5:18 PM
D_Stark
D_Stark - avatar
+ 3
Tashi N when we create an object of child and the parent holds the refrenece to that object what class are we calling the methods from parent or child? because am thinking child but now I'm thinking its parent but it must be child right? 😅 Tnx
14th Jul 2019, 6:02 PM
D_Stark
D_Stark - avatar
+ 3
D_Stark There is no way that a super class knows it's child class. But with the keyword extends you make it possible that a child class knows its super class. There is a hirarchy (one direction): Parent class -> child class -> ... Imagine a class which can save stuff. Now you want a class which can save and load. You just extend the Save class and have all what you need. class SaveAndLoad extends Save The Save class can save, the SaveAndLoad class can save and load. Why should the Save class also can load if you already have one class which can both? By the way cycling inheritance is not allowed. class Mother extends Child{ } class Child extends Mother{ }
14th Jul 2019, 9:00 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Thanks for the answers! I appreciate it
15th Jul 2019, 2:10 AM
Shadow Shadow
Shadow Shadow - avatar
+ 2
Tashi N sry is it right to think that the variable of type parent holds a refrence that points to the child object but will only be able to access the methods in the child object that are inherited from parent it extends anything else is hidden from the parent? Sorry I'm just trying to build a clear picture in my mind it's the only part I have to trouble understanding 😅
14th Jul 2019, 6:56 PM
D_Stark
D_Stark - avatar
+ 2
Denise Roßberg hey, just looking at your comment again and noticed that you said a sub class knows about its parent but this is not true, only base class knows about its sub classes because of this you cannot create a sub type refrence variable refer to a super class instance .
22nd Jul 2019, 10:13 PM
D_Stark
D_Stark - avatar