Junaid S.
Junaid S.

Reputation: 2642

Polymorphism and Virtual method

Assume that the Vehicle class contains a virtual method named CalculateMaxSpeed. Assume that both the MotorVehicle and Automobile classes override this method. Which class defines the method that is called when the second statement in the code that follows is executed?

Vehicle (grandparent of Automobile) > MotorVehicle (parent of Auatomobile) > Automobile

MotorVehicle car = new Automobile();
car.CalculateMaxSpeed();

Well in my point of view it should be Automobile#CalculateMaxSpeed but I am afraid that it can be MotorVehicle#CalculateMaxSpeed becasue MotorVehicle is containing the instance of Automobile. Please somebody elaborate.

Upvotes: 4

Views: 382

Answers (2)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

Either if you type a reference with the concrete class or a base class, typing has nothing to do with what to call unless you use identifier reusing:

public class A 
{
     public virtual string Text { get; set; }
}

public class B : A
{
     // "new" keyword is identifier reusing. You're
     // defining a new member which uses Text again and
     // hides "Text" to references typed as B
     new public string Text { get; set; }
}

public class X : A
{
     public override string Text { get; set; }
}


B someB = new B();
someB.Text = "hello world";

// Now try to access Text property... what happened?
// Yes, Text property is null, because you're reusing the 
// identifier in B instead of overriding it
A someBUpcastedToA = someB;
string text = someBUpcastedToA.Text;

X someX = new X();
someX.Text = "goodbye";

// Now if you access someXUpcastedToA.Text
// property it will give you the "goodbye" string
// because you're overriding Text in X instead of
// reusing the identifier
A someXUpcastedToA = someX;

At the end of the day, typing is giving more or less metadata to an object reference and it provides access to current type members, but the object stored in the so-called reference is still the same one, either if it's more or less typed.

Think about typing like showing or hiding details of a given object:

// someX reference provides access to all X members
X someX = new X();

// This is upcasting. Now it's still the X instance but
// with access to less members. 
A a = someX;

Overrides are just reusing a method or property signature (i.e. public string Text) and changing its body in a derived class. When a method is marked with either virtual or abstract, the compiler and runtime knows that it's a polymorphic class member, and runtime will call the most specific implementation for a given polymorphic member. This is the reason behind the fact that typing doesn't change the object boxed by a reference.

Upvotes: 0

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

Your understanding is correct. Automobile#CalculateMaxSpeed will be invoked. This is called Runtime Polymorphism.

Though the type of the car object is MotorVehicle, at runtime, content of the object will be identified as derviced class type which is Automobile. So, the method will be invoked not based on the type of the object, but on the content of the object.

Type will be used by the compiler, but the actual decision of invoking the method is done during run time based on the content of the object.

Upvotes: 2

Related Questions