Ok, bear with me guys and girls as I\'m learning. Here\'s my question.
I can\'t figure out why I can\'t override a method from a parent class. Here\'s the code fro
In C# methods are not virtual by default, so if you design some method as overridable, you should specify it as virtual:
class Base
{
protected virtual string GetMood() {...}
}
Second, you have to specify that you are going to override method from base class in derived class.
class Derived : Base
{
protected override string GetMood() {...}
}
If you don't specify "override" keyword, you will get method that hides base type (and warning from compiler to put "new" keyword for the method to explicitly state so).
If you want to stop inheritance chain and disallow further overrides of the method, you should mark method as sealed, like this:
protected sealed override string GetMood() {...}
As far as I know, in Java all methods are virtual by default. This is not the case with C#, so you need to mark the base class methods with "virtual", e.g. protected virtual string getMood() ...
and the overrides with "override", e.g. protected override string getMood()...
.
By making your method GetMood virtual
in Moody and override
in the derived classes, your code should work. Are you sure you placed these keywords where they belong ? Here is the complete code, that compiles and run just fine :
public class MoodyObject
{
protected virtual String getMood()
{
return "moody";
}
public void queryMood()
{
Console.WriteLine("I feel " + getMood() + " today!");
}
}
public class SadObject : MoodyObject
{
protected override String getMood()
{
return "sad";
}
//specialization
public void cry()
{
Console.WriteLine("wah...boohoo");
}
}
public class HappyObject : MoodyObject
{
protected override String getMood()
{
return "happy";
}
public void laugh()
{
Console.WriteLine("hehehehehehe.");
}
}
class Program
{
static void Main(string[] args)
{
MoodyObject moodyObject = new MoodyObject();
SadObject sadObject = new SadObject();
HappyObject happyObject = new HappyObject();
Console.WriteLine("How does the moody object feel today?");
moodyObject.queryMood();
Console.WriteLine("");
Console.WriteLine("How does the sad object feel today?");
sadObject.queryMood();
sadObject.cry();
Console.WriteLine("");
Console.WriteLine("How does the happy object feel today?");
happyObject.queryMood();
happyObject.laugh();
Console.Read();
}
}
Maybe your mistake comes from the fact that in Java, all methods are virtual, which is not the case is C# (as pointed out by Dan C.).
If you want to override a base class method, it needs to be declared as virtual
. The overriding method in a derived class has to be explicitly declared as override
. This should work:
public class BaseObject
{
protected virtual String getMood()
{
return "Base mood";
}
//...
}
public class DerivedObject: BaseObject
{
protected override String getMood()
{
return "Derived mood";
}
//...
}
Edit: I just tried it in a c# console application, and it compiles. So the source code you use should differ in some tiny but important piece from what you posted here.
My program.cs is this:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// leaving out your implementation to save space...
}
}
public class SadObject : MoodyObject
{
protected override String getMood()
{
return "sad";
}
//specialization
public void cry()
{
Console.WriteLine("wah...boohoo");
}
}
public class HappyObject : MoodyObject
{
protected override String getMood()
{
return "happy";
}
public void laugh()
{
Console.WriteLine("hehehehehehe.");
}
}
}
public class SadObject: MoodyObject
{
override String getMood()
You need to mark the overrides of getMood with the "override" keyword. You'll also need to mark the base getMood method with the "virtual" keyword.