You have a class named Truck that inherits from a base class named Vehicle. The Vehicle
class includes a protected method named brake ().
How should you call the Truck class implementation of the brake () method?
A.
Vehicle. brake ();
B.
This. brake ();
C.
MyBase. brake();
D.
Truck. brake ();
Explanation:
The MyBase keyword behaves like an object variable referring to the base class of the
current instance of a class.MyBase is commonly used to access base class members that
are overridden or shadowed in a derived class.
XD wrong. There is no such thing as MyBase keyword!! Seriously… its B. this.Brake();
“MyBase” is Visual Basic style corresponding to the “base” keyword in C#.
But I agree “this” would be the best answer here (or “Me” in VB.NET).
Are you sure it isn’t D. Truck.brake(); ?
For starters, the proper case of the keyword is this, not This. It doesn’t specify where you calling the Truck class implementation from. If you are in the truck class then I agree that this.brake(); would be fine, but outside of it, wouldn’t you have to use Truck.brake(); ?
Vehicle.break() i think
This question is really confusion. Regardless what key word it is using This instead of this, and MayBase instead of base, I don’t even understand what the question is asking!
“How should you call the Truck class implementation of the brake () method?”
call from where? call what?
1. Inside of the implementation of Truck’s brake method to call Vehicle’s brake method, then use base.brake();
2. Inside of Truck’s class to call Truck’s break method, use this.brake();
3. Inside of Truck’s class to call Vehicle’s break method, use base.brake();
No mater what we cannot choose A or D. even you call from outside of the class – use object name intead of class name, unless it is static method.
So, I think this question is not clear. Any one have any idea?
Following code has been tested.
====================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine(“Hello, world!”);
Truck tr = new Truck();
tr.Test();
//following line cause compile error if uncomment
//Truck.Test();
}
}
public class Vehicle
{
protected void brake()
{
Console.WriteLine(“Vehicle.brake()”);
}
}
public class Truck : Vehicle
{
protected void brake()
{
//following line cause compile error if uncomment
//Vehicle.brake();
base.brake();
Console.WriteLine(“Truck.brake()”);
}
public void Test()
{
Console.WriteLine(“base.brake()”);
base.brake();
Console.WriteLine(“this.brake()”);
this.brake();
}
}
}
====================================================================
Out put here
——————————————————————–
Hello, world!
base.brake()
Vehicle.brake()
this.brake()
Vehicle.brake()
Truck.brake()
Agree with Tony (very well expressed and demonstrated). I believe that the question is asking how to refer to the base class within a derived class. Whether VB (MyBase), or C# (base). that’s my understanding. Answer gives credence to my point?