Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

internal abstract methods. Why would anyone have them?

I was doing some code review today and came across an old code written by some developer. It goes something like this

public abstract class BaseControl
{
    internal abstract void DoSomething();
}

If you have a derived class within the same assembly, it would work

public class DerivedControl : BaseControl
{
    internal override void DoSomething()
    {
    }
}

But deriving the base class in a different assembly would give compile time error

DerivedControl does not implement inherited abstract member 'BaseControl.DoSomething()

That got me thinking. Why would anyone declare a method as internal abstract ?

like image 419
ram Avatar asked Feb 20 '10 20:02

ram


People also ask

Why do we need abstract methods?

In any programming language, abstraction means hiding the irrelevant details from the user to focus only on the essential details to increase efficiency thereby reducing complexity. In Java, abstraction is achieved using abstract classes and methods. Let's get to know more about the abstract method in Java.

Can abstract class be internal?

An abstract class may have private, internal, static non abstract-members. An abstract method is implicitly a virtual method. A non-abstract class does not have an abstract method. It means an abstract method can only be declared with an abstract class.

Should abstract methods be public?

abstract methods have the same visibility rules as normal methods, except that they cannot be private .


1 Answers

The original programmer wanted to make a derived control available to client code. But prevent the client from inheriting and messing with the virtual method. That's not a bad idea, it is usually easy to break a base class by overriding a method and doing something like forgetting to call the base class method.

like image 145
Hans Passant Avatar answered Sep 21 '22 10:09

Hans Passant