user430481
user430481

Reputation: 315

Don't allow grandchildren of abstract classes to override their parent c#

// This class just offers a public interface for triggering events
public abstract class TriggerActivator
{
    public void ActivateTrigger(){
        OnTriggerActivate();
    }
    protected abstract void OnTriggerActivate();
}

// This class does some important work, but looks for specific information returned from its child
public abstract class RaycastTriggerActivator : TriggerActivator
{
    protected override void OnTriggerActivate()
    {
        // Do some important raycast-related stuff...
        bool specificImportantInfo = SpecifyImportantInfo();
        // Do some more stuff...
    }
    protected abstract bool SpecifyImportantInfo();
}

// This class basically gives the parent info it needs based on a specific input type
public class MouseRaycastTriggerActivator : RaycastTriggerActivator
{
    protected override bool SpecifyImportantInfo() => IsMouseButtonPressedDown();
}
// OR
public class ControllerRaycastTriggerActivator : RaycastTriggerActivator
{
    protected override bool SpecifyImportantInfo() => IsControllerButtonPressedDown();
}

BUT, someone could break this functionality easily:

public class MouseRaycastTriggerActivator : RaycastTriggerActivator
{
    protected override bool SpecifyImportantInfo() => IsMouseButtonPressedDown();

    /// It is important for this class's parent to implement this method,
    /// but now this class is hiding its parent's implementation
    protected override void OnTriggerActivate()
    {
        /// This guy can hide RaycastTriggerActivator's functionality
        /// and break the whole system
    }
}

I could see this happening if someone doesn't understand the system well enough, sees OnTriggerActivate in the list of available functions to override, and thinks that they need to use it.

My question is, if B : A, and A has an abstract method meant for B to implement, is there a way to hide that abstract method from C : B, if the method is specifically not meant for C to provide implementation for? (":" = "inherits from")

Am I worrying about this too much? I don't see how it could be a security risk for the entire program.

Upvotes: 0

Views: 500

Answers (1)

chris-crush-code
chris-crush-code

Reputation: 1149

You can use the sealed keyword to prevent C from overriding the method implemented by B like so:

    public abstract class A
    {
        protected abstract void SomeFunction();
    }

    public class B : A
    {
        protected override sealed void SomeFunction()
        {
            //do something
        }
    }

now if you try to implement SomeFunction() in C like so:

public class C : B
    {
        protected override void SomeFunction()
        {
            //do something different
        }
    }

You'll receive an error in the IDE and you won't be able to compile:

'C.SomeFunction()': cannot override inherited member 'B.SomeFunction()' because it is sealed

Upvotes: 4

Related Questions