- Home /
2 Polymorphism Questions (I'm beginner : c )
Hi, thank you for your time!!
CONTEXT:
public abstract class Vehicle
{}
class Truck : Vehicle
{
public int TowingCapacity;
}
Q1:
I tried to do this:
Vehicle myTruck = new Truck();
myTruck.TowingCapacity = 1200;
I got an Error: Vehicle does not contain a definition for TowingCapacity.
Why can't I access TowingCapacity?
Q2:
I tried to do this:
Truck myTruck = new Vehicle();
An error tells me: Cannot create an instance of the abstract class "Vehicle".
Why is it that I can do
Vehicle myTruck = new Truck ();
but not
Truck myTruck = new Vehicle();?
Much Appreciated!!!
Q2: Vehicle is abstract, that means you can't make one directly (you can only make the non-abstract types that derive from it).
Q1: The Vehicle class doesn't have the towingCapacity variable, it's only in Truck.
So what you can do is this...
Truck myTruck = new Truck();
myTruck.TowingCapacity = 1200;
I suggest you get yourself a book on C#
Thanks Bonfire! Yeah I am following some tutorials at the moment, in fact it gave the exact answer as you did. But tutorials often talks about how things should be done and don't talk about why they shouldn't be done other ways, and so I was just curious about the scenarios that the tutorial didn't talk about.
If your C# book doesn't explain these points, then I would seriously consider throwing it away and finding one that does.
Could you recommend a book to me please? Thanks!! (I've read through Learning C# Program$$anonymous$$g with Unity 3D btw, but it only briefly talks about some ideas and doesn't go very deep into them)
For C#, don't bother with any of the "for Unity" stuff. It's like if you wanted to learn to drive so you can get to church. You'd just take regular driving lessons. You can practice on totally different streets and parking lots; when you get to the church ones, they're pretty much the same. Program$$anonymous$$g is the same way.
I've been told that the Head First series isn't too bad. But just search "learn C#".
Answer by imen · Feb 09, 2015 at 03:48 PM
Hello, Q1 : The Vehicle class is a parent class. It can't access to thier child's method and attribute. You can do a simple cast
Vehicle myTruck = new Truck();
((Truck)myTruck).TowingCapacity = 1200;
Q2 : In Object-oriented programming (OOP) you can't instantiate abstract class, because it can have none implemented method.
Answer by Owen-Reynolds · Feb 09, 2015 at 03:59 PM
All of this (inheritance, virtual keywords, exact error messages... ) is standard C#, and there are many non-Unity sites with examples, explanations, places to ask... . They tend to be much more thorough and extensive than anything you'll find on UA.
C# in Unity really does work 99.99% the same as every other use of C#. Even for things like a list of gameObjects. GameObjects are Unity only, but keeping a list of [whatever] is a standard C# topic, which many well-explained non-Unity examples which work the same way in Unity.
Your answer
Follow this Question
Related Questions
foreach Element Problem 1 Answer
How to access derived class members from base class type 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Polymorphism issue 3 Answers
Abstract class question 1 Answer