C
C#•4mo ago
Marius 🗡

Is there a way to block the calling of public method from abstract class inside a derived one?

It makes sense to just not call it, but is there a way to prevent it? example:
public abstract class Abstract
{
public void Launch() { } // calls some inner private methods belonging to the abstract class itself does work with its data.
}

public class Derived : Abstract
{
private void SomeMethod()
{
Launch(); // <-- is it possible to block such call?
}
}

// i would use that launch method in other outer class that uses other specific derived classes and calls the launch method like this:

public class Foo()
{
public void DoWork()
{
Launch<Derived>();
}

void Launch<T>() where T : Abstract
{
Abstract specificClass = (T)Activator.CreateInstance(typeof(T));
specificClass.Launch();
}
}
public abstract class Abstract
{
public void Launch() { } // calls some inner private methods belonging to the abstract class itself does work with its data.
}

public class Derived : Abstract
{
private void SomeMethod()
{
Launch(); // <-- is it possible to block such call?
}
}

// i would use that launch method in other outer class that uses other specific derived classes and calls the launch method like this:

public class Foo()
{
public void DoWork()
{
Launch<Derived>();
}

void Launch<T>() where T : Abstract
{
Abstract specificClass = (T)Activator.CreateInstance(typeof(T));
specificClass.Launch();
}
}
8 Replies
Salman
Salman•4mo ago
if you don't wan it to be able to be called then simply make it private and don't even give the access to it 🤷
Sossenbinder
Sossenbinder•4mo ago
Indeed. I also don't quite get the use case. Are you looking to block the calls internally?
Becquerel
Becquerel•4mo ago
if you want some child classes to use a method in the abstract parent class, but not others, your inheritance design is flawed think about the liskov substitution principle the parent class should specify behaviour common to all children
Salman
Salman•4mo ago
indeed
Marius 🗡
Marius 🗡OP•4mo ago
Thank you for responding! Yes, I wanted to prevent that method to be called internally in the derived class. Which sounds stupid, asked just out of curiosity, it has no use case, and the solution to it is to not make stupid calls. And this question itself derives from lack of OOP knowledge, which also makes my current design flawed. I kind of wanted to use Launch() as a main method for the class workflow and only call it from the outside. Calling that method inside the class again would recursively launch the same logic. I guess I was curious about a design that would prevent stupid usage of a method. But I guess the solution to that would be to just not to do stupid things. Sorry if my deluded question wasn't clear
Becquerel
Becquerel•4mo ago
ahh, no worries it's hard to tell sometimes between people who are just learning OOP, and those who are intent on abusing it to do bizarre things
Anton
Anton•4mo ago
you can always achieve almost any idea, you just need even more classes to represent everything
Marius 🗡
Marius 🗡OP•4mo ago
Appreciate the response!
Want results from more Discord servers?
Add your server