Inheritance
Inheritance is a way to form new classes (instances of which will be objects) using pre-defined objects or classes where new ones simply take over old ones' implementations and characteristics. It is intended to help reuse of existing code with little or no modification. en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

An illustrated example:
Here is a basic Bird class with some get and set functions and a show the wingspan function:

This next file describes an aquatic bird, and extends the basic bird properties and functions:

The file below calls the functions in the above files. Notice that you can call a function that was created in the basic Bird class for an AquaticBird object. This is the concept of inheritance.
- inheritance enables reuse of classes
- more generic classes (base classes) pass on characteristics to more specific classes (vehicle: base class, truck: more specific type of vehicle -- both have some of the same basic characteristics)

- The keyword extends means "inherits".
- Class SharkDog extends Dog.
- In the above diagram the subclass (aka child class) SharkDog gets all of the properties that are in the class dog, and then any extras that are specific to the class SharkDog.
- Another good thing about inheritance is that all child classes of a parent can be treated as a parent.
- E.G. SharkDog and DragonDog (both whom extend the class Dog) can be treated as Dogs and use the Dog methods and properties.
- Anything that changes in the dog class (the parent class) will be applied to all of the classes that extend it.
An illustrated example:
Here is a basic Bird class with some get and set functions and a show the wingspan function:

This next file describes an aquatic bird, and extends the basic bird properties and functions:

The file below calls the functions in the above files. Notice that you can call a function that was created in the basic Bird class for an AquaticBird object. This is the concept of inheritance.


0 Comments:
Post a Comment
<< Home