Restricting subclasses from inheriting certain methods of base class - c#

using System;
public class Base
{
public Base()
{
}
public void M1()
{
}
public void M2()
{
}
public void M3()
{
}
}
public class Derived : Base
{
//this class should get only method 1
}
public class SecondDerived : Base
{
//this class should get only method 2 and method3
}
The requirement is : the base class contains the 3 methods M1, M2, M3.
The derived class should inherit only M1 and SecondDerived should inherit only M2 and M3.
How can this be done?

You cannot selectively inherit methods like this. A derived class automatically inherits all public methods of the base class. I suggest you to split the Base class into two classes:
public class Base1
{
public Base1()
{
}
public void M1()
{
}
}
public class Base2
{
public void M2()
{
}
public void M3()
{
}
}
public class First : Base1
public class Second : Base2

You cannot do it in this way. Inheritance implies an "IS A" relationship.
If SecondDerived would not have a M1() then it would not be compatible with a reference to a the class Base.
So maybe you shouldn't be using inheritance for whatever problem you're solving.

It is not possible to do what you want with inheritance.
It seems you have no intention of overriding, you simply want to "inherit" behavior from the base class selectively. You could do this using a "has a" relationship:
public class Base
{
internal Base() {} //mark constructor as internal so it can not be used outside your assembly if necessary
public Foo Mehtod1() {...}
public Foo Mehtod2() {...}
public Foo Mehtod3() {...}
}
Then simply do the following:
class A
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method1() { return this.internalBase.Method1(); }
}
class B
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method2() { return this.internalBase.Method2(); }
public Foo Method3() { return this.internalBase.Method3(); }
}
UPDATE: A possible alternative solution is to make your Base class methods virtual and override them all in your derived classes, throwing NotSupportedExceptions in those methods that you do not want the class to make available. I don't really like this solution but it has the advantage of not loosing the polyphormism inheritance gives you which might be useful if you have some core base functionality which all derived classes will share (in your example you seem to imply they wont).

It is possible by adding Obsolete attribute
public class A
{
public virtual void M1() { }
public void M2() { }
public void M3() { }
}
public class B : A
{
[Obsolete("You can not use this", true)]
public sealed override void M1()
{
}
}
public class C : B
{
public void Test()
{
// Will show error
base.M1();
}
}

Related

Dependency injection with abstract class in .NET Core

I don't know how to use a dependency injection in an abstract class. Let me show you my problem in a simple example:
public abstract class Animal {
public abstract void Move();
public void Sleep()
{
restService.StartSleeping(1000); //how to get this service here?
}
}
public class Cat : Animal
{
public readonly IMotionService _motionService;
public Cat(IMotionService motionService)
{
_motionService = motionService;
}
public override void Move()
{
_motionService.Run();
}
}
public class Bird : Animal
{
public readonly IMotionService _motionService;
public Bird(IMotionService motionService)
{
_motionService = motionService;
}
public override void Move()
{
_motionService.Fly();
}
}
Every animal move in different way so the Move() function is implemented separately in every derived class. As you probably noticed the whole implementation comes from the motionService.
On the other hand all animals sleep in same way, so I want put the Sleep() implementation in a base abstract class to avoid a duplication code, but I can't use my restService with a Sleep implementation because I don't have idea how to inject a service class into an abstract class.
I thought about IServiceProvider but it should be injected too.
You pass it down like this:
public abstract class Animal
{
private readonly IRestService restService;
public Animal( IRestService restService )
{
this.restService = restService;
}
public abstract void Move();
public void Sleep()
{
restService.StartSleeping(1000);
}
}
public class Cat : Animal
{
// vv Should be private!
public readonly IMotionService _motionService;
public Cat(IMotionService motionService,
IRestService restService)
: base(restService) // pass on to base class ctor
{
_motionService = motionService;
}
public override void Move()
{
_motionService.Run();
}
}
// Same in `Bird` class
For reference: Using Constructors (C# Programming Guide)

Member can't be protected if used "fluently", even though it shouldn't be used outside

I have the following example:
class A {
public A DoSomethingInternal() {
// Some work..
return this;
}
}
class B : A {
public void DoSomething() {
DoSomethingInternal().DoSomethingInternal().DoSomethingInternal();
}
}
DoSomethingInternal is a method that should not be called by the outside objects. It should only be accessible to A and the inheritors of A - so it sounds like it should be protected.
However, due to the fact that DoSomethingInternal is a "fluent" method, I cannot make it protected.
A solution that I see is:
class A {
public A DoSomethingInternal() {
// Some work..
return this;
}
}
class B : A {
public void DoSomething() {
((B)(((B)DoSomethingInternal()).DoSomethingInternal())).DoSomethingInternal();
}
}
but I find it very inelegant to require derived classes to do these casts.
You can "tell" base class about derived class as a generic type argument.
public abstract class A<T> where T : A<T>
{
protected T DoSomethingInternal()
{
// Do something
return (T)this;
}
}
public class B : A<B>
{
public void DoSomething()
{
// Do something
this.DoSomethingInternal().DoSomethingInternal();
}
}

access the original implementation of a method from a class inheriting an abstract method

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.

Template method pattern without inheritance

How can a variant of the Template Method pattern be implemented whereby the concrete class does not inherit from the base class, but the overall feature of the pattern is maintained. The reason it cannot inherit is that it's forced to inherit from another class and multiple-inheritance is unavailable.
For example, suppose the following Tempate Method pattern:
public abstract class BaseClass {
public void Alpha() {
Beta();
}
public abstract void Beta();
public void Gamma() {
Delta();
}
public abstract void Delta();
}
public ConcreteClass : BaseClass {
public override void Beta() {
Gamma();
}
public override void Delta() {
Console.WriteLine("Delta");
}
}
...
var object = new ConcreteClass();
object.Alpha(); // will outout "Delta"
How can I achieve the same result without ConcreteClass inheriting BaseClass?
Your base class could depend on an interface (or other type) that's injected via the constructor. Your template method(s) could then use the methods on this interface/type to achieve the pattern's desired outcome:
public class BaseClass
{
IDependent _dependent;
public BaseClass(IDependent dependent)
{
_dependent = dependent;
}
public void Alpha() {
_depdendent.Beta();
}
public void Gamma() {
_depdendent.Delta();
}
}
Effectively using composition rather than inheritance.
You can achieve this by providing a reference to the base class on method call:
public ConcreteClass {
public void Beta(BaseClass baseClass) {
baseClass.Gamma();
}
public void Delta() {
Console.WriteLine("Delta");
}
}

Inheriting from a class that inherits from a abstract class

I trying to inherit a class Blah2, but after adding a method it says BlahA doesn't implement that method.
How can I add a method to my new class?
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyNewMethod()
{
}
}
Note: BlahA is an abstract class.
Update
public abstract class BlahA : IBlah
{
}
Update II - the error
Error 3 'Blah.Components.BlahA' does not contain a definition for 'Blah3' and no extension method 'Blah3' accepting a first argument of type 'Blah.Components.BlahA' could be found (are you missing a using directive or an assembly reference?)
Well if it's implementing an interface as you posted in your comments, then the problem is that your BlahA class doesn't satisfy the requirements of the interface. There must be some method in the interface (I'm assuming its the MyNewMethod) that you're not implementing in your abstract BlahA class.
If my assumption is correct, add this to your base class:
public abstract List<int> MyNewMethod();
and in your sub class, add the word override to your method declaration.
Some code:
public interface MyInterface
{
void MyMethod();
}
public abstract class Base : MyInterface
{
public abstract void MyMethod();
}
public class SubA : Base
{
public override void MyMethod()
{
throw new NotImplementedException();
}
}
public class SubB : SubA
{
public void Foo() { }
}
Wrting this code and compiling works fine
public abstract class BlahA
{
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
return new List<int>();
}
}
We will need a bit more of the code that isnt working
EDIT:
from comments you need to implement the method from interface in abstract class.
public interface IBlah
{
int GetVal();
}
public abstract class BlahA : IBlah
{
public int GetVal()
{
return 1;
}
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
int i = GetVal();
return new List<int>();
}
}

Categories

Resources