0

am familiar with abstract classes, interfaces, and the like.

What is not clear to me is why anyone would create a C# abstract class that does not have abstract members? (the C# compiler allows this).

Example:

public abstract class House
{
    public void OpenDoor()
    {
        Console.WriteLine("Door opens");
    }
}
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
  • 2
    I think more code would be required in the compiler to spot this "issue" and prevent it. So the question can be reversed - why should the compiler be *specifically* designed to prevent it? – Damien_The_Unbeliever Aug 07 '13 at 07:16
  • Indeed a valid question. And in my opinion, this is a sign of bad design. Usually I have done this in my past, and in the end I need to mark `OpenDoor` as virtual because my derived houses need specific method for it. One alternative is to add another method `OnOpenDoor` that is virtual protected, allowing the children to have specific implementation while having control of `absolute` implementation from base class. – Fendy Aug 07 '13 at 15:21

11 Answers11

4

You may need a common base to access to refer to so you can process a list of many different types of houses. You could have a List<House> and some of those could be BrickHouse others WoodenHouse but opening the door functions the same for all houses so it makes sense to put the method in the base. You declare the base abstract if it does not make sense to instantiate that class.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
2

simply because: you are expected to subclass it?

Maybe in this context, House simply isn't specific enough; sure, all things in this relationship are houses, but it is expecting concrete types such as Bungalow, Mansion, Apartment, etc... So House serves as a useful categorisation (for example, in a List<House> or a property of type House) - but you would never expect to have an actual instance of that common base type.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • If expected to subclass, why not marking it as virtual? Opening door in `Bungalow` can be different from one in `Apartment`. – Fendy Aug 07 '13 at 15:27
  • @Fendy that depends on the real example - not all methods are best virtual. Some are, some aren't. – Marc Gravell Aug 07 '13 at 16:17
  • In this case I refering the `OpenDoor` as real example though. And IMHO, abstract class without abstract / virtual member is a bad design. – Fendy Aug 07 '13 at 16:29
1

I could imagine that some platform (perhaps in a company) might define an abstract class programmers should subclass so that methods can be added in later versions?

Andrew J. Brehm
  • 4,448
  • 8
  • 45
  • 70
1

Just to auto implement things for them, so for example provide a default implementation of GetHashCode, or maybe ToString. Also, it allows for you to add a method to an interface that the abstract class implements, provide a default implementation, and then nothing breaks, instead of the other option where every class breaks and you have to fix all of them manually.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
1

Interestingly, static classes in C# are in fact sealed and abstract ones. The purpose in this very case is clear: allowing to declare static methods only.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Generalization

Abstract classes can be a conceptual 'Interface'. This is different from a .NET interface. A generalized interface example is:

// List that accepts the conceptual interface
List<Car> cars = new List<Car>();

// Specialized instance that will be added to the list
Car myCar = new Toyota();
cars.Add(myCar);

Inheritance

You can implement multiple interfaces but inherit just from one abstract class. This can be a design decision made by the developer of the class library.

Usage

Dhananjay wrote a nice post about the usage. He states that abstract classes are used for modelling your class hierarchy. Interfaces however are used for communication. That the object actually is does not matter.

Community
  • 1
  • 1
Myrtle
  • 5,761
  • 33
  • 47
  • Say that I want to has a class named `Fishman`. Is it inherited from `Fish`, or is it inherited from `Mammal`? – Fendy Aug 07 '13 at 15:43
0

So that the base class defines the implementation. If the Implementation is common across all the derived classes it makes sense to keep it in the abstract class not duplicate the code in the derived class.

See this link, has some useful info about this http://www.codeproject.com/Articles/6118/All-about-abstract-classes

lloydom
  • 377
  • 2
  • 11
0

abstract classes, at all, are more of a logic class to inherit from rather than use it as basic for inheritance, you can use abstract class to tell all who inherits from it to have a certain property or a specific method to do, Interfaces can do exactly that.

Lets say I have a mammal class, but a mammal class is just an abstract class, there is no real animal that is of a kind of a mammal, although there are many animals ( including humans) that inherit "properties" from it.

Again, abstract use is definitely up for the developer choice.

Rayzond
  • 11
  • 3
0

Abstract classes with abstract members are meant to be used polymorphically. Abstract classes with only concrete members would be used if you wanted to share common methods with subclasses, perhaps even a specific construction sequence (as in having the abstract class's constructor do setup that's "housekeeping").

It's a weaker form of coupling than polymorphic subclasses, and I can't think of a time I've run across it myself.

DavidN
  • 5,117
  • 2
  • 20
  • 15
0

Abstract class serves as a template (may or may not provide default implementations) while child could overwrite anything he does not like, or additional methods/properties/etc.

a very interesting scenario is type constraint inheritance:

public abstract class MyClass
{
    public void DoSomething()
    {
        Console.WriteLine("blah blah");
    }
}

public class MyClass<T>: MyClass
{
    public T GetSomething()
    {
        // return null as T;
    }
}

you may see the default implementation is at the base class.

Rex
  • 2,130
  • 11
  • 12
  • This is indeed a common pattern for lists of `MyClass` objects because the generic classes otherwise have no shared base class that can be used (except object). – Myrtle Aug 07 '13 at 07:32
0

Often the base class is used to provide just common functionality. Depending on your design this common functionality can be implemented without dependencies on members. In this case usually the subclasses don't need any common members. In you example the House class of objects is used for simple actions and I assume you don't need any common state of the house such as number of floors, type of walls (wood, bricks, etc.). Seeing such a class makes me think "OK, this class isn't holding any state of the object. It just performs some action"

stan0
  • 11,549
  • 6
  • 42
  • 59