Here's my situation:
interface Icontainer{
string name();
}
abstract class fuzzyContainer : Icontainer{
string name(){
return "Fuzzy Container";
}
}
class specialContainer: fuzzyContainer{
string name(){
return base.name() + " Special Container";
}
}
Icontainer cont = new SpecialContainer();
cont.name(); // I expected "Fuzzy Container Special Container" as the output.
When I run my code as described above the output is simply "Fuzzy Container". What am i missing here? Is there a better way to get the desired results?
A base class is also called parent class or superclass. Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class.
The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
One reason for this could be that BaseClass is abstract (BaseClasses often are), you want a BaseClass and need a derived type to initiate an instance and the choice of which derived type should be meaningful to the type of implementation.
If you make name() virtual
and then override
in specialContainer, you will get the expected behavior.
public interface Icontainer
{
string name();
}
public abstract class fuzzyContainer : Icontainer
{
public virtual string name()
{
return "Fuzzy Container";
}
}
public class specialContainer : fuzzyContainer
{
public override string name()
{
return base.name() + " Special Container";
}
}
First off, it is helpful when you have questions that you post code that actually compiles. It is difficult to analyze a problem when it is full of missing modifiers and typos; it is hard to know whether the problem is the typo or not.
Once we do the work to fix up your program so that it actually compiles, the compiler emits a warning that tells you that the overloading looks wrong. Since your question is "why is the overloading wrong?" it would probably be a good idea to read the compiler warning that we emitted precisely so that you can analyze this problem.
The problem is that the derived class contains a new method called "name", not an override of the existing method. That's what the warning is trying to tell you.
There are two ways to fix this problem, depending on whether you intended the method to be "new" or "override". If you intended the method to be "override" then make the base implementation virtual and the derived implementation override.
If you intended the method to be "new" and you still want the new method to replace the binding to the interface implementation then use interface reimplementation:
class SpecialContainer: FuzzyContainer, IContainer
{
public new string Name()
{
return base.Name() + " Special Container";
}
}
Notice the "new" and the fact that we have re-stated that this class implements IContainer. This tells the compiler "disregard the bindings to methods of IContainer deduced from the base class and start over."
This is because you are hiding the method and not overriding it. Without making the base method virtual it isn't possible to override it, but you can "hide" it by creating a method of the same name.
If you had:
SpecialContainer cont = new SpecialContainer();
cont.name();
Then you will also get the output you expected.
Matt's answer also works and may be preferable if you can edit the base class to make the method virtual. By default methods are not overridable unless explicitly marked as such with the virtual
keyword
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With