Question:
How should you call the Glass class implementation of the break() method?
Example
In this example they have an class named Glass
. This class has a method called Break()
that comes from the base class Window
.
They want you to call the implemented method in the Glass class
"call the Glass class implementation of the break() method"
To create an own version of a base class method you need to make it over writable. To do this add virtual to the base class method and in the derived class Glass
add override to override that base class method.]
Next you can call different version of the method the derived method and base method. See Example for more details
This example will look like this:
class Window
{
public virtual void Break()
{
// break method from the window class
}
}
class Glass : Window
{
public override void Break()
{
// This method comes from the base class Window. You want to override this one. They ask you to call this method.
//To call the Break() mehod from Window:
base.Break();
// Call the Break() method from the current instance
this.Break()
Break();
}
}
Answer:
This answer is right because this one calls the current instance off the Glass class Break() method
(See example)
C: this.break();
Other answers:
The Break()
is not static because that would not make any sense and can't be made static in this question. They want a Glass to inherit from Window and want to call the Break()
version of the Glass class. You need to override the Break()
in the Glass class to create an own version of that method, Because of that you need to add virtual and override and virtual/override methods can not be made static. Because of that the first two answers are incorrect
A Window.Break()
This will call the static Break() Method from the Window class. (static is not used in this example this is not the answer)
B Glass.Break()
This will call the static Break() Method from the class. (static is not used in this example this is not the anwer)
C this.Break()
This will call the current instance Break() Method (See Example).
D base.Break()
This will call the Break() method from the current instance of base class Window.