Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good example for class inheritance? [closed]

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?

like image 564
Tim Jansen Avatar asked Feb 22 '09 16:02

Tim Jansen


People also ask

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 class inheritance example?

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.

What is inheritance in Java with example?

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.

How is class inheritance used?

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.


1 Answers

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.

like image 143
Jon Skeet Avatar answered Oct 12 '22 03:10

Jon Skeet