I have a base and a derived class as below:
//base class
public class BST
{
public virtual void Find();
public virtual void Insert()
{
Find();
}
}
// derived class
public class splayTree:BST
{
public override void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
After I created a splayTree object splayTree,
I call splayTree.Insert();
Insert() calls base.Insert()
base.Insert() calls Find() of the derived class.
How can I let base.Insert() call Find() of base class?
Thank you.
The base class has defined Find as Virtual, which is a signal that the child class may override it and if so, the base class will use that override.
If you want to call the base Find in your child class, just do not implement Find.
If you want to define a method called Find in your child class, that is not virtually connected to base.Find, use the new keyword instead of the override keyword.
By not overriding the Find method:
//base class
public class BST
{
public virtual void Find();
public virtual void Insert()
{
Find();
}
}
// derived class
public class splayTree:BST
{
public void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
If you want to call the base class' Find() method but still retain flexibility of having Find() be a virtual method with derived implementations, break out the logic of the base.Find() method into a private method you can call explicitly from the base class:
//base class
public class BST
{
private void _Find()
{
// base.Find() implementation here
}
public virtual void Find()
{
_Find();
}
public virtual void Insert()
{
_Find();
}
}
// derived class
public class splayTree:BST
{
public override void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
Related
I have a situation where I want to call only one method and have all the methods its derived from called also.
public class Base
{
public virtual void Method()
{
// Do something
}
}
public class Derived1 : Base
{
public override void Method()
{
base.Method();
// Do something
}
}
public class Derived2 : Derived1
{
public override void Method()
{
base.Method();
// Do something
}
}
I want to only call Derived2.Method() and have all the Methods() called that it derives from
The issue with the way I currently have it set up is that Derived1.Method() never gets called.
Is there a way to make the called method call all the previous instances of it in a derived class?
public class BaseConcrete
{
public virtual void DoSomething()
{
// Original implementation.
}
}
public abstract class BaseAbstract: BaseConcrete
{
public abstract override void DoSomething();
}
public class SubConcrete: BaseAbstract
{
public override void DoSomething()
{
// New implementation.
}
}
How do I access the original DoSomething() method implementation of BaseConcrete class? a base call does not work here in this case. Any other approach? Thanks in advance.
I have a merly simple question, but seems cant find an answer for it, I want to know if its possible to override a method from a instance class structore would look like this:
public class A : baseA
{
public virtual void methodA()
{
}
}
public class B : baseB
{
public void method B()
{
var ClassA = new A();
}
/* Now Is there some sort of overide like */
public override methodA()
{
//Do stuff
}
}
And those classes do not inherit from each other, to make it more difficult.
Now if this sort of construction is possible in c#?
No. You cannot override a class's behavior if you don't inherit from it.
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
Class B must inherit from class A in order to do so.
public class A
{
public virtual void methodA()
{
}
}
public class B : A
{
public void methodB()
{
var ClassA = new A();
}
public override void methodA()
{
//Do stuff
}
}
Check MSDN for more details:
An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method
I am trying to create a comprehensive abstract BaseClass that defines the way in which all derived classes are created, but allows derived classes to specialize/aggregate the fields and methods used in the creation process. Here is a simplified example:
public abstract class BaseClass
{
public List<String> list;
public BaseClass()
{
defineList();
optionalDoSomething();
doSomething();
}
protected void defineList()
{
list = new List<String>();
}
protected void doSomething()
{
// do something w/ list here
}
protected void optionalDoSomething() {}
}
public class DerivedClass : BaseClass
{
protected void defineList()
{
base.defineList();
list.Add("something");
}
public DerivedClass() : base() { }
}
public class SecondDerivedClass : DerivedClass
{
protected void defineList()
{
base.defineList();
list.Add("somethingElse");
}
protected void optionalDoSomething()
{
// do something special
}
public SecondDerivedClass() : base() { }
}
This would free all derived classes from having to recreate the same initialization logic, and each derived class would only need to "overwrite" the necessary fields and methods used in the create process (and possibly elsewhere in the class).
The problem:
I cannot mark BaseClass' methods as virtual since you cannot call virtual methods in a base constructor (in any case, I would not want to use virtual methods since, for example, I would not want DerivedClass to use SecondDerivedClass' defineList method).
I can mark them abstract, but then I would not be able to put "default implementations" in BaseClass and each derived class would have to replicate/implement those defaults. Also, SecondDerived class would still need a way to "override" the implementations of DerivedClass.
It does not work to simply use the new key word "hide" less derived class' methods.
What is the correct way to obtain this pattern?
TLDR: as per my comment below:
If BaseClass is an abstract class with method A, and DerivedClass is a class derived from BaseClass (not necessarily a direct child of BaseClass), then calling A in BaseClass' constructor should call A() in every class in the inheritance hierarchy up to and including DerivedClass (but no further). We can assume that A (forced to be) defined on every intermediate class.
I think that you should refer to template-method-design-pattern
Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. Template Method lets subclasses redefine certain
steps of an algorithm without changing the algorithm's structure.
you can try something similar to this
abstract class AbstractClass
{
public List<String> list;
public abstract void PrimitiveOperation1();
public void TemplateMethod()
{
//initialize code that each class should perform
PrimitiveOperation1();
}
}
class DerivedClass: AbstractClass
{
public override void PrimitiveOperation1()
{
list.Add("something");
}
}
usage
AbstractClass abstractClass1 = new DerivedClass();
abstractClass1.TemplateMethod();
Try this solution, the implementation is implemented with protected virtual methods, so its not visible from the outside and not required in derived classes:
public abstract class BaseClass
{
public List<String> List { get; protected set; }
protected BaseClass()
{
defineList();
optionalDoSomething();
doSomething();
}
protected void defineList()
{
// default implementation here
List = new List<String>();
internalDefineList();
}
protected void doSomething()
{
// default implementation here
internalDoSomething();
}
protected void optionalDoSomething()
{
// default implementation here
internalOptionalSomething();
}
protected virtual void internalDefineList()
{
}
protected virtual void internalDoSomething()
{
}
protected virtual void internalOptionalSomething()
{
}
}
public class DerivedClass : BaseClass
{
protected override void internalDefineList()
{
var list = List;
}
protected override void internalDoSomething()
{
}
// this method is not required
/*
protected override void internalOptionalSomething()
{
}
*/
}
One way to achieve what you want is to add an explicit Initialize method to the base class and do the initializaiton logic there, e.g:
public abstract class BaseClass
{
public List<String> list;
public BaseClass()
{
}
public void Initialize()
{
defineList();
optionalDoSomething();
doSomething();
}
}
I want to be able to run the method in my base class which is displaySomething()
and in addition run the method in my subclass called displaysomething()
Is this possible? How can I achieve this please?
I have a base class that looks like this
public class baseClass
{
public void displaySomething()
{
MessageBox.Show("Method from base class: display something");
}
}
I have a subclass that looks like this
public class subClass : baseClass
{
public void displaySomething()
{
MessageBox.Show("Additional method to run after base class method");
}
}
I have a button click event that looks like this.
private void button1_Click(object sender, EventArgs e)
{
subClass mySubClass = new subClass();
mySubClass.displaySomething();
}
You need to add the call of the overriden method of the base class in the implementation of the overriding method of the subclass, like this:
public void displaySomething() {
// You can decide to call the base before, after,
// or in the middle of your new method.
base.displaySomething();
MessageBox.Show("Additional method to run after running base class method");
}
Note that your code does not override the method, because it is not declared virtual. You need to declare it like this:
public class baseClass {
public virtual void displaySomething() ...
}
public class subClass : baseClass {
public override void displaySomething() ...
}