...

/

Solution Review: Override a Method in the Derived Class

Solution Review: Override a Method in the Derived Class

This review provides a detailed analysis to solve the 'Override a Method in the Derived Class' challenge.

We'll cover the following...

Solution

Press + to interact
// Base Class
class Product {
// Private Data Members
private string _className;
public Product() { // Default Constructor
this._className = "Product";
}
// Getter Function
public virtual string GetName() {
return this._className;
}
}
// Derived Class
class XProduct : Product {
private string _className;
public XProduct(string className) { // Default Constructor
this._className = className;
}
// Overriden Method
public override string GetName() {
return base.GetName() + ", " +this._className;
}
}
class Demo {
public static void Main(string[] args) {
Product beverage = new XProduct("Beverage");
Console.WriteLine(beverage.GetName());
}
}

Explanation

...