Polymorphism : BaseClass X = new DerivedClass(); vs DerivedClass X = new DerivedClass(); | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 7

Polymorphism : BaseClass X = new DerivedClass(); vs DerivedClass X = new DerivedClass();

To instantiate X of a derived class, you can use BaseClass X = new DerivedClass(); or DerivedClass X = new DerivedClass(); https://code.sololearn.com/c3B9jaAmceDW I am using DerivedClass X = new DerivedClass(); What is the best way to instantiate a object of derived class ? And why ? Why would you use BaseClass X = new DerivedClass();

14th Feb 2019, 9:09 PM
sneeze
sneeze - avatar
10 Respuestas
+ 7
Yes I think this is the difference between C# and Java. To get the same result as in Java I believe you have to use the virtual and override keywords in C#. https://code.sololearn.com/c4t8Rqxtj9Sf/?ref=app
16th Feb 2019, 1:03 AM
Sonic
Sonic - avatar
+ 5
The Box BaseClass contains the object DerivedClass(). Let this be Box A. The Box DerivedClass contains the object DerivedClass(). Let this be Box B. Despite containing a DerivedClass(), the box has been disguised as a BaseClass(). Therefore, take this example BaseClass method run() -> prints "Hi" DerivedClass method run() -> prints "Bye" So Box A, which behaves like a BaseClass, uses the BaseClass run(). Box B, which behaves like it should, uses the DerivedClass run()
14th Feb 2019, 11:41 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
sneeze How about this? class Shape, class Square derived from Shape class Circle derived from Shape array of Squares and Circles Then when you iterate through the array, which is of type Shape btw, you can access the common properties.
16th Feb 2019, 5:22 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
In C# you have a difference between Base and Derived Class when you instantiate the object, as many people prove it in this thread, so you should always instantiate as the proper class if you have overridden methods. In Java, this doesn't happen; actually you get the overridden result whatsoever, even if it was instantiated as an Object. So the reason why you should instantiate an object as a BaseClass is if you have to pass it as parameter in a method; and you should instantiate it as DerivedClass if you have to call a method that is declared only in the DerivedClass.
16th Feb 2019, 5:01 PM
Alessio Giliberti
+ 3
@Prometheus. You are right. I made some code to prove it. https://code.sololearn.com/cxK8e615OeTq Is there a case where someone would use BaseClass X = new DerivedClass();
15th Feb 2019, 8:03 PM
sneeze
sneeze - avatar
+ 2
I was wondering the same, so I tried to code your example but I have the same output for the 2 objects, so I still don't see the difference https://code.sololearn.com/c7fiLXDAo0A9/?ref=app
15th Feb 2019, 8:34 AM
Tanguy Leconte
Tanguy Leconte - avatar
+ 2
@BrakTan I have looked at your code and also do not see the difference, like I see in my code. Is this a difference between c# and Java ?
15th Feb 2019, 8:06 PM
sneeze
sneeze - avatar
+ 2
A common use I found about this is if you want to have an array of different objects from different derived classes like an array of Mercedes benz car 🚗, Honda motor 🏍 and Collins bus 🚌, they all must have the same type for example Vehicle and that's the reason why we create classes car, motor and bus as Vehicle() class and instantiate them as they own car(), motor() and bus() classes.
16th Feb 2019, 4:01 PM
AI Machine
AI Machine - avatar
+ 2
@AI-Machine Now I understand the example. You need to typecast alot to get to each objects own methods though https://code.sololearn.com/c3hr9LhbFNp8 Glad we have generic list nowadays.
16th Feb 2019, 8:51 PM
sneeze
sneeze - avatar
+ 2
After some research I find that, in Java, when you BaseClass X = new DerivedClass(), you can change the type of the subclass after instantiate it, if the DerivedClass is derived from the same BaseClass https://code.sololearn.com/cjXjq61BpD6k/?ref=app It may be the same in C#. It may be a reason to use BaseClass X = DerivedClass in some case.
17th Feb 2019, 6:57 AM
Tanguy Leconte
Tanguy Leconte - avatar