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)?
An abstract class is a template definition of methods and variables of a class (category of objects) that contains one or more abstracted methods. Abstract classes are used in all object-oriented programming (OOP) languages, including Java (see Java abstract class), C++, C# and VB.NET.
Abstract methods are displayed by italic text (UML has a boolean value for this). The notation you are using is called Keyword (simple way) or Stereotype (more complex).
(Note: In UML, abstract methods/classes are denoted by slanted text, specialization is denoted by an arrow with a solid line, and realization/implementation of an interface is denoted by an arrow with a dashed line.)
An abstraction of a system is a model of the system that includes all the details about the system. UML diagrams are a form of abstraction, since they hide details and allow us to concentrate just on the major design components.
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!
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With