0

Downcasting

If we dont downcasting the object and recall the method that exist in superclass and override in subclass which one is our output?(superclass method OR subclass method)

23rd Apr 2020, 4:19 PM
Amirhossein Zamanzade
Amirhossein Zamanzade - avatar
2 Answers
+ 4
Hello Amirhossein Zamanzade It calls the subclass method. Imagine you have a super class Mom and a sub class Kid. Let's say Kid has two methods, one own funOwn() and own inherited from Mom but overriden funOverride() The object is from type Kid but treated as Mom: Mom mo = new Kid(); mo.funOwn() -> it will not find this method because Mom has not such a method mo.funOverride() -> Mom has such a method, but Kid overrides it -> Kid's method is invoked Let's say Mom has also an own method: funMom() mo.funMom(); -> Mom has such a method, but Kid does not override it -> Mom's method is invoked. I hope this helps you.
23rd Apr 2020, 6:06 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Here is a code where you can see it: https://code.sololearn.com/cvaj8l2KGrzd/?ref=app Btw: when you down cast mo to Kid, than it is just a normal object from type Kid.
23rd Apr 2020, 6:27 PM
Denise Roßberg
Denise Roßberg - avatar