Background:
As a Java programmer, I extensively inherit (rather: implement) from interfaces, and sometimes I design abstract base classes. However, I have never really felt the need to subclass a concrete (non-abstract) class (in the cases where I did it, it later turned out that another solution, such as delegation would have been better).
So now I'm beginning to feel that there is almost no situation where inheriting from a concrete class is appropriate. For one thing, the Liskov substitution principle (LSP) seems almost impossible to satisfy for non-trivial classes; also many other questions here seem to echo a similar opinion.
So my question:
In which situation (if any) does it actually make sense to inherit from a concrete class? Can you give a concrete, real-world example of a class that inherits from another concrete class, where you feel this is the best design given the constraints? I'b be particularly interested in examples that satisfy the LSP (or examples where satisfying LSP seems unimportant).
I mainly have a Java background, but I'm interested in examples from any language.
Concrete class is not having abstract keyword during declaration. Abstract class can inherit another class using extends keyword and implement an interface. Interface can inherit only an inteface.
For instance, we are humans. We inherit certain properties from the class 'Human' such as the ability to speak, breathe, eat, drink, etc. We can also take the example of cars. The class 'Car' inherits its properties from the class 'Automobiles' which inherits some of its properties from another class 'Vehicles'.
Represents an inheritance hierarchy of classes with one table per concrete class in the hierarchy. For a full description see P of EAA page 293. As any object purist will tell you, relational databases don't support inherit-ance - a fact that complicates object-relational mapping.
An abstract class always extends a concrete class ( java. lang. Object at the very least). So it works the same as it always does.
You often have a skeletal implementations for an interface I
. If you can offer extensibility without abstract methods (e.g. via hooks), it is preferable to have a non-abstract skeletal class because you can instantiate it.
An example would be a forwarding wrapper classes, to be able to forward to another object of a concrete class C
implementing I
, e.g. enabling decoration or simple code-reuse of C
without having to inherit from C
. You can find such an example in Effective Java item 16, favor composition over inheritance. (I do not want to post it here because of copyrights, but it is really simply forwarding all method calls of I
to the wrapped implementation).
I think the following is a good example when it can be appropriate:
public class LinkedHashMap<K,V> extends HashMap<K,V>
Another good example is inheritance of exceptions:
public class IllegalFormatPrecisionException extends IllegalFormatException public class IllegalFormatException extends IllegalArgumentException public class IllegalArgumentException extends RuntimeException public class RuntimeException extends Exception public class Exception extends Throwable
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