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)?
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)?
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:
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:
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.
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.
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.
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.
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 ).