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.)
Answer: See the 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.
third box is:
((Logger)logger).LogCompleted();
Correct!!! I tested this:
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log continuing”);
((Logger)logger).LogCompleted(); // Display ‘Finished’
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log continuing”);
logger.LogCompleted(); // Display ‘Completed’
Answer is correct. no other other option gives expected result.
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log continuing”);
logger.LogCompleted();
abstract class BaseLogger
{
public virtual void Log(string message)
{
Console.WriteLine(“Base: ” + message);
}
public virtual void LogCompleted()
{
Console.WriteLine(“Completed”);
}
}
class Logger : BaseLogger
{
public override void Log(string message)
{
Console.WriteLine(message);
}
public override void LogCompleted()
{
Console.WriteLine(“Finished”);
}
}
ok, confirmed.
The code run as expected
thanks
Your implentation is not same as in the qustion. Instead “override LogCompleted()” in the question is “new LogCompleted()”, so last box must be cast in the Logger before invoke the method LogCompleted() , only so the result is right . Point thing is in the keyword “new” .
conc. keyword new :
@see https://www.amazon.com/C-6-0-Nutshell-Definitive-Reference/dp/1491927062/ ch.3, p.93 f “Hiding Inherited Members”
this is working too:
BaseLogger logger = new Logger();
logger.Log(“Log started”);
((BaseLogger)logger).Log(“Log continuing”);
logger.LogCompleted();
This is correct
@Riccardo
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log continuing”);
((Logger)logger).LogCompleted(); // Display ‘Finished’
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log continuing”);
logger.LogCompleted(); // Display ‘Completed’