5

In an UML diagram, when should a class be abstract? Just when we want to prevent instantiation?

Edit: Can an abstract class fully implement a method (when the method only depends on the attributes of that abstract class)?

John Assymptoth
  • 8,227
  • 12
  • 49
  • 68

7 Answers7

7

Preventing instantiation is a matter of implementation, so it is a good reason if you are thinking in terms of Java, C# etc.

However, on the level of modelling, you'd want to make a class abstract if there are several subclasses, and it makes no sense that an object is of the supertype but of neither of the subtypes.

So, basically you're not preventing instantiation of the class, since any instance of the subclass is also an instance of the abstract superclass. What you are doing in that case is preventing instantiation of the superclass alone, i.e. enforce using one of the subtypes.

Answer to EDIT: Yes!

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
  • +1 For the comment on subtypes and supertypes - does seem to be an academic question as a result. – amelvin Feb 05 '11 at 14:21
  • @amelvin: don't see how it's academic? @Goran is right. If superclass marked abstract then no object can be an instance of the supertype alone - it must be an instance of a subtype too. That's an important distinction. – sfinnie Feb 06 '11 at 08:58
  • @sfinnie Fair point; I mean academic in that I think this is an academic edge case rather than a real-world problem. The OP in another comment states that his abstract class has no methods, so its an abstract class without implementation, surely an interface would be a better fit in a real world example? – amelvin Feb 06 '11 at 18:59
  • @sfinnie then again, if a class has some static members then there is a good reason for it to be abstract - http://stackoverflow.com/questions/4901364/should-a-base-class-be-marked-abstract-if-there-are-no-abstract-members – amelvin Feb 06 '11 at 19:48
  • @amelvin: an abstract class with no declared methods (abstract or otherwise) sounds suspect. Whole point of abstract superclass is to highlight commonality among subclasses. If there's no commonality, why have superclass? Same principle holds for an interface with no methods. – sfinnie Feb 06 '11 at 20:41
  • 1
    @amelvin, @sfinnie: True, it wouldn't make much sense in a real-world use. I've seen interfaces without methods being called marker interfaces, they were used to add extra info to the implementing classes in Java before annotations were added to the language (I know it's language specific, but maybe that's the case here). If a class has only static methods, like a utility class, then I guess it would be pointless to extend it. On the other hand, if the members are only constants, then interface would still do the job. – Goran Jovic Feb 06 '11 at 21:06
6

A class is abstract if it has one or more operations that are abstract.

In UML indicate abstract with italics or {abstract}.

The following diagram (which is on page 70 of my UML Distilled Third Edition book by Martin Fowler) shows how and why to denote abstracts: enter image description here

amelvin
  • 8,919
  • 4
  • 38
  • 59
  • 1
    Your first sentence is incorrect. It's the other way around - to have abstract method the precondition is that the enclosing class is abstract. You may have an abstract class with all concrete methods and it would still be abstract if defined as such. – Goran Jovic Feb 05 '11 at 13:03
  • 1
    @Goran Sorry, but the first sentence is a direct quote from Martin Fowler's book UML Distilled Third Edition, p69, third last paragraph. This question is about UML, not specifically OOP - some programming languages would allow an abstract class with all concrete methods - some may not. In UML terms an abstract class is typically a class that has one or more abstract methods - not the other way round. – amelvin Feb 05 '11 at 13:10
  • What Goran said. My abstract class will actually have no methods. Not everything that is written is correct. **ESPECIALLY** in Software Engineering. I've seen a lot of garbage in books/papers. – John Assymptoth Feb 05 '11 at 13:12
  • 1
    Interesting. I always thought that it was about inheritance cardinality. Thanks for the clarification. – Goran Jovic Feb 05 '11 at 13:19
4

Abstract class a is basic OOP concept so I don't think your question is specific to UML.
Here is a question on when to use abstract classes:

Exact use of Abstract class

Basically, you should use abstract class when several classes share a set of functionality but that functionality doesn't make sense or is incomplete on its own. Only abstract classes can contain abstract methods, which are for descendants to implement.

For example, you might want to extract basic FileLogger and EmailLogger functionality into Logger class, however since it doesn't make sense on its own, you just leave it abstract.

Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
0

A class should be abstract when you have multiple classes with same functionality, but some methods of each of this class is different from the super class (the abstract one).

e.G. An animal. Each animal-class has the method isAlive(). This method is equal for all animal. And each animal has the method move(target). But each animal moves in a different way. You can't talk of the movement of an animal in a general way. Some animals fly, some swim and some run.

Tobi
  • 540
  • 7
  • 18
0

When you're trying to express subclassing, like when you want to make explicit a Cat is one special case of Animal.

For instance, Animal could have some basic implementation on some behaviors, like Eat, which could be override on subclasses.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

An abstract class is like a regular class except that it allows you to define method headers without a method body.

This allows derived classes to have their own implementation without the base class having it's own implementation.

Aesir
  • 2,033
  • 1
  • 28
  • 39
0

well an abstract class means that you can maintain a list of methods or variables(public static final by default inside abstract class) this enforces the class extending this abstract class to implement those methods not implemented in the declared abstract class, (abstract class has a high similarity to interfaces , but the only difference is that interfaces are 100% abstract classes ).