Accessing subclass method from superclass C#

sb_

So I know I can't actually access methods and attributes of a subclass from it's parent. But I have a program which stores Vehicles in an array (NOT arraylist - its homework). The Vehicles then initialize into either Airplane, Boat, or a Car object derived from the Vehicle class. These subclasses have attributes which are unique, and I'm wondering as to how I could go about accessing these?

Here's some (simplified) relevant code:

Vehicle[] vehicles = new Vehicle[20];

vehicles[0] = new Airplane();

// Setting an attribute of the superclass
vehicles[0].Make = "Boeing";

// Set an attribute from the Airplane class
vehicles[0].Engine = "Jet"; //(obviously this doesn't work)

How could I go about working around this? I've researched for a couple hours but I'm stumped by this one problem.

Thanks :)

CodeCaster

You can access a reference through the type of its declaration. Because you declare the array to be of Vehicle, you cannot directly access subclass members.

To do so, initialize the object separately from the collection that contains it:

var jet = new Airplane();
jet.Make = "Boeing";
jet.Engine = "Jet";

vehicles[0] = jet;

Alternatively, use an object initializer:

vehicles[0] = new Airplane
{
    Make = "Boeing",
    Engine = "Jet"
};

It would be quite nonsensical to cast it back just after instantiating it, but you could do that as well:

vehicles[0] = new Airplane();
((Airplane)vehicles[0]).Make = "Boeing";
((Airplane)vehicles[0]).Engine = "Jet";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing subclass method from arraylist of superclass

From Dev

c++ calling method from subclass in superclass

From Java

Calling a subclass method from superclass

From Java

Accessing Scanner objects from subclass in a superclass?

From Dev

Method defined in superclass but implemented in subclass: accessing subclass attributes?

From Java

Why is "this" from superclass calling method from subclass?

From Dev

Java:Method called from subclass in superclass constructor

From Dev

Access Django SubClass Method from SuperClass

From Dev

Calling a Subclass method from an array of Superclass Objects

From Dev

Python way of accessing superclass members from a subclass object

From Dev

Accessing subclass variables in a superclass in Ruby

From Dev

Accessing a protected value of a superclass argument in a subclass of the superclass

From Dev

Superclass method with subclass data

From Java

Is there a benefit from having a subclass method that only calls the overridden superclass method?

From Dev

How to call an overriden superclass method from within a decorated subclass method

From Dev

How to call inherited method of superclass from the overridden subclass method?

From Dev

Why is it not overriding my method in superclass by another method from subclass?

From Dev

Accessing variable of a method in superclass

From Dev

C# accessing subclass method by casting

From Dev

How do you call a subclass method from a superclass in Java?

From Dev

Why is my value null when calling superclass method from subclass?

From Java

java - Calling a subclass method from dynamically casted superclass

From Dev

How to reference subclass from a static superclass method in Groovy

From Dev

Calling a subclass method from an ArrayList of type superclass in Java

From Dev

How can I call superclass method with data from subclass?

From Dev

Forcing a subclass to call its superclass method in Objective-C

From Dev

Optimize Superclass Method Depending on Subclass

From Dev

declaring a subclass in superclass and method calling

From Dev

Superclass method obtain subclass variable

Related Related

  1. 1

    Accessing subclass method from arraylist of superclass

  2. 2

    c++ calling method from subclass in superclass

  3. 3

    Calling a subclass method from superclass

  4. 4

    Accessing Scanner objects from subclass in a superclass?

  5. 5

    Method defined in superclass but implemented in subclass: accessing subclass attributes?

  6. 6

    Why is "this" from superclass calling method from subclass?

  7. 7

    Java:Method called from subclass in superclass constructor

  8. 8

    Access Django SubClass Method from SuperClass

  9. 9

    Calling a Subclass method from an array of Superclass Objects

  10. 10

    Python way of accessing superclass members from a subclass object

  11. 11

    Accessing subclass variables in a superclass in Ruby

  12. 12

    Accessing a protected value of a superclass argument in a subclass of the superclass

  13. 13

    Superclass method with subclass data

  14. 14

    Is there a benefit from having a subclass method that only calls the overridden superclass method?

  15. 15

    How to call an overriden superclass method from within a decorated subclass method

  16. 16

    How to call inherited method of superclass from the overridden subclass method?

  17. 17

    Why is it not overriding my method in superclass by another method from subclass?

  18. 18

    Accessing variable of a method in superclass

  19. 19

    C# accessing subclass method by casting

  20. 20

    How do you call a subclass method from a superclass in Java?

  21. 21

    Why is my value null when calling superclass method from subclass?

  22. 22

    java - Calling a subclass method from dynamically casted superclass

  23. 23

    How to reference subclass from a static superclass method in Groovy

  24. 24

    Calling a subclass method from an ArrayList of type superclass in Java

  25. 25

    How can I call superclass method with data from subclass?

  26. 26

    Forcing a subclass to call its superclass method in Objective-C

  27. 27

    Optimize Superclass Method Depending on Subclass

  28. 28

    declaring a subclass in superclass and method calling

  29. 29

    Superclass method obtain subclass variable

HotTag

Archive