• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ComeSweetDeath · Feb 09, 2015 at 03:02 PM · castingpolymorphism

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!!!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Feb 09, 2015 at 03:10 PM 2
Share

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#

avatar image ComeSweetDeath · Feb 09, 2015 at 03:57 PM 0
Share

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.

avatar image Bonfire-Boy · Feb 09, 2015 at 04:04 PM 1
Share

If your C# book doesn't explain these points, then I would seriously consider throwing it away and finding one that does.

avatar image ComeSweetDeath · Feb 09, 2015 at 04:13 PM 0
Share

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)

avatar image Owen-Reynolds · Feb 09, 2015 at 06:28 PM 1
Share

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#".

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ComeSweetDeath · Feb 09, 2015 at 03:55 PM 1
Share

Thanks for the casting tip!

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Inspector - editing array of polymorphic objects? 2 Answers

ScriptableObject : Serialized Polymorphism Classes Can Not be Deserialize with Polymorphism 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges