DRAG DROP
You are developing a C# console application that outputs information to the screen. The following code
segments implement the two classes responsible for making calls to the Console object:
When the application is run, the console output must be the following text:
Log started
Base: Log continuing
Finished
You need to ensure that the application outputs the correct text.
Which four lines of code should you use in sequence? (To answer, move the appropriate classes from the list
of classes to the answer area and arrange them in the correct order.)
Select and Place:
Explanation:
Note:* The abstract keyword enables you to create classes and class members that are incomplete and must be
implemented in a derived class.
* An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition
of a base class that multiple derived classes can share.
Everything correct except the last box. It should be option 4: “((Logger)logger).LogCompleted()”.
The variable logger is a reference to class BaseLogger and the LogCompleted method is not virtual, which means that to call the LogCompleted from the Logger class you have to make an explicit cast. This was tested in a console application.
You are right.
Besides, Logger.LogCompleted uses the new modifier, whch means that this method is not overriden, but completely new -> So you have to cast to Logger in order to use LogCompleted.
See also https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-modifier
tested and logger.LogCompleted() also works
but the message is “Completed”
Sure it “works”, as it compiles and runs – but it does not deliver the desired output, does it.
((Logger)logger).LogCompleted() is correct as the last line
Tested and verified:
Baselogger logger = new Logger();
logger.Log(“Log Started”);
logger.Log(“Base: Log continuing”);
((Logger)logger).LogCompleted();
Correct!
yeah “public new void” hides the method instead of overriding, so a cast is necessary.
check here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords