I'm writing documentation for an object-oriented language, and I wonder what kind of classes would be a good example for inheritance.
Some common examples:
class Person {
}
class Employee extends Person {
}
Currently my favorite, but I don't like Person->Employee because 'Employee' does not exactly look like fun.
class Bicycle {
}
class MountainBike extends Bicycle {
}
I found this in some Java tutorial, but it's not really obvious what attributes a bike should have.
class Animal {
}
class Bird extends Animal {
}
Same as the bicycle.
class A {
}
class B extends A {
}
Too abstract. The main problem is that such a class will need even more abstract attributes and methods.
Does anyone have a better example for a simple class hierarchy?
I suggest 'devices'. Nobody really models animals using software, but they do model devices.
I always was fond of:
But any of the top three you quote would be fine. MountainBike sounds the most exciting. You can do similar things with cars of course.
What about
et.
I like the
Stream
hierarchy. The idea is that anything can use a stream without usually caring what kind of stream it is, and individual subclasses handle the storage differently (e.g.NetworkStream
,MemoryStream
andFileStream
in .NET).If you're interested in interfaces, then
IEnumerable<T>
in .NET is a great one - you can iterate over any collection without caring what the underlying data structure is.What about a hierarchy of algebraic expressions. It is an excellent example because it uses both inheritance and composition:
Then you can provide a motivating example like:
Which would yield:
I think Shape is a good abstract class. There are both 2D and 3D shapes. The 2D shapes typically have area while 3D shapes have volume. Both can have a "location" or "mass center".
Some suggestions: