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?
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'.
In this, various Child classes inherit a single Parent class. The example given in the introduction of the inheritance is an example of Hierarchical inheritance since classes BMW and Audi inherit class Car. Here two child classes are inheriting the same Parent class.
Inheritance in Java is a concept that acquires the properties from one class to other classes; for example, the relationship between father and son. Inheritance in Java is a process of acquiring all the behaviours of a parent object.
Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces.
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
and FileStream
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.
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