Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good examples of inheriting from a concrete class? [closed]

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.

like image 518
sleske Avatar asked Sep 21 '11 07:09

sleske


People also ask

Can a concrete class be inherited?

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.

What is a good example of inheritance?

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'.

What is concrete inheritance?

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.

Can an abstract class inherit from a concrete class?

An abstract class always extends a concrete class ( java. lang. Object at the very least). So it works the same as it always does.


2 Answers

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

like image 157
DaveFar Avatar answered Sep 21 '22 18:09

DaveFar


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 
like image 36
Andrey Avatar answered Sep 21 '22 18:09

Andrey