1

I have two questions regarding the use of an abstract class on left side of object instantiation.

AbstractClass MyClass = new ConcreteClass();

1 - What is this called? (when you declare the abstract class on the left.)

e.g.

Car MyNiceCar = new NiceCar();

I know this relates to Polymorphism, but I'm specifically asking how to describe/verbalize the scenario when declare the abstract class on the left.

2 - And why do it? i.e. Why would you do:

Car MyNiceCar = new NiceCar();

And not:

NiceCar MyNiceCar = new NiceCar();

?

Would the answer to question 2 possibly be so that i can do the following?

Car MyNiceCar = new NiceCar();

.
. //do some logic to decide if I can have a nicer car.
.

MyNiceCar = new EvenNicerCar();
fourbeatcoder
  • 1,159
  • 3
  • 13
  • 21
  • First, Please ask one question per post. Second, The subject of polymorphism can be explained in an answer, but it too broad for stackoverflow. You should probably read a good blog post or tutorial about it – Zohar Peled Sep 25 '18 at 09:55
  • @ZoharPeled My questions are not a generic question about the concept of Polymorphism. They are two very specific questions about 1) how to verbalize something specific I have seen in code and 2) What the benefits/reasons are of a specific approach. Both questions are very much related. Therefore I think my post is of value here. User Mars answer below demonstrates this. – fourbeatcoder Sep 25 '18 at 10:36
  • One problem when one post have more than one question is that - What if you get two great answers, but each answer is for a single question? How can you choose the best answer, or what answer to accept? – Zohar Peled Sep 25 '18 at 11:16
  • @ZoharPeled I would accept answers that answer both parts of my post. I see similar posts with two questions all over S/O. For example, this is the highest voted C# post on S/O and it asks two questions: "Guidelines for use" and "Differences": https://stackoverflow.com/q/7074/976537 . What would be very helpful would be if you submitted and answer to my question please. – fourbeatcoder Sep 25 '18 at 11:27
  • You know what, I don't care. Do whatever you want. Forget I even wrote anything. – Zohar Peled Sep 25 '18 at 11:39
  • 2
    Forget who wrote what? – Full Time Skeleton Sep 25 '18 at 15:05

3 Answers3

4

1) You're creating a base class reference to your Derived class.
Edit:
Other words for BaseClass: SuperClass, ParentClass
Other words for DerivedClass: SubClass, ChildClass Don't ask why there are so many words for each. It's kinda a Spaces vs Tabs type thing.

2) You do it so that you can use virtual functions/properties that you know all the derived classes will have. You want to do something that a Car can do, and you don't care if its a CrapCar, NiceCar or SuperNiceCar

Car car = new MyNiceCar();
car.honk(); //meep!
car = new SuperNiceCar();
car.honk(); //beep beep!

However, you can't go the other way around.

SuperNiceCar may support UseTurbo(), but MyNiceCar does not.

But you don't care, you just want the car to honk, so you cast it as a Car, because you know all Cars can honk.

See also

Mars
  • 2,505
  • 17
  • 26
  • 2
    Thanks for this answer, it explains a lot very clearly. I have one question though, your use of the term "pointer", is that not a concept from C++ that does not exist in C#? Can you help me understand better by rephrasing or using a different word? – fourbeatcoder Sep 25 '18 at 09:27
  • Sorry, the correct term is reference, not pointer! I fixed it! – Mars Sep 26 '18 at 01:03
  • 1
    Thanks for clarifying that the term is "reference" – fourbeatcoder Sep 27 '18 at 08:07
2

1 - What is this called?

This is a type of polymorphism that's called "inclusion polymorphism". It allows you to create restrictions on the type of variable that can be used in your code, while still allowing some type flexibility. Java and C#, for instance, have generics that allow various types and references to use the same code. However, these often run into run-time issues that could otherwise be caught by a static code analyzer. It's generally considered to be less risky to rely on the static analyzer (assuming your IDE has a good one) than to wait and find out your code has bugs in it after your release.

2 - And Why do it?

In addition to the reason I gave above, one of the most common applications of this is the polymorphic array.

Consider the following:

// Both classes contain an Accelerate() function
NiceCar MyNiceCar = new NiceCar();
EvenNicerCar MyEvenNicerCar = new EvenNicerCar();

// We're forced to use these methods separately
MyNiceCar.Accelerate();
MyEvenNicerCar.Accelerate();

////////////////////////////////////////////////////////////////////////////////////////////////////

// We can make this code much smaller and less prone to copy/paste errors using polymorphism!
Car[] MyPolymorphicArray = new Car[] { new NiceCar(), new EvenNicerCar() };
foreach(c in MyPolymorphicArray) { c.Accelerate(); }

Using this type of syntax, you can see that I'm able to make the code smaller, more-manageable, type-safe, and our static code analyzer will complain if we get it wrong, rather than waiting until runtime to find out. When you use this type of architecture, make sure you require the methods intended to be used by the instances to either be in an interface or in the parent class of that object in order to help avoid implementation errors. Additionally, take note that if you do this, the Accelerate() method may only need to be written in the parent class, which helps make the code even smaller.

Chris Mauer
  • 176
  • 1
  • 8
  • 2
    Thank you. Excellent answer. I had not studied the concept of Inclusion polymorphism. Good to know. One tiny detail though, I was asking specifically what is it called when you write the following: `Car car = new MyNiceCar();` - Apparently this is called "Upcasting" – fourbeatcoder Sep 27 '18 at 08:10
  • I had always thought upcasting was more in the form of Car car = (Car)MyEvenNicerCar;, but I guess that really would be the same thing as if you were instantiating it to be of type Car. That's what I love about this site; you learn something new everyday, even if it's just minor details like that. – Chris Mauer Sep 27 '18 at 14:15
2

Following on from two excellent answers from @Mars and @chris-mauer which both answered Question 2 of my post perfectly. And allowed me to study this concept further.

I want to include that the answer to Question 1 of my post as follows. I asked:

AbstractClass MyClass = new ConcreteClass();

1 - What is this called? (when you declare the abstract class on the left.)

From the article below, I believe the correct answer is:

Upcasting

For more on the overall concept, which is Inclusion Polymorphism, I studied the following article: http://www.msdotnet.co.in/2014/05/how-to-implement-polymorphism-concepts.html

fourbeatcoder
  • 1,159
  • 3
  • 13
  • 21