Can i override an abstract method written in a parent class, with a different name in a child class?
abstract class SettingSaver
{
public abstract void add(string Name, string Value);
public abstract void remove(string SettingName);
}
class XMLSettings : SettingSaver
{
public override void add(string Name, string Value)
{
throw new NotImplementedException();
}
public override void remove(string SettingName)
{
t开发者_StackOverflowhrow new NotImplementedException();
}
}
Is there anyway that I can change the name of the add function in XMLSettings class to addSetting but make sure it overrides the add function in the SettingSaver? I know it should be definitely overridden in derived classes but just want to know whether I can use a different name :) Thanx in advance :D
No, you can't change the name when you override a method in C#.
Of course, you can override the method and implement it just by calling a different method.
(On a naming convention note, methods are typically PascalCased, starting with a capital letter. Parameters are typically camelCased, starting with a lower case letter.)
No you can't.
You can add another method that houses the same functionality, but you can't hide or rename the inherited method.
For an example of this the Stream class in the framework has a method called Close(). This is exactly the same as calling Dispose(), but given a name that is more in line with the concept of "opening" a steam to start using it. They haven't removed the Dispose() method, only given the client another (better named) way to access the same functionality.
No - that would somewhat violate Polymorphism (anything the parent can do, the children should be able to do).
You could create a new method and use metadata on the old method to direct anybody developing against the class to use the new class.
The method name and arguments is what identifies a method. If you change the method name, then you are identifying a different method. Since overriding is about defining the same method in a subclass, then you must use the same method name.
精彩评论