Difference between revisions of "Inheritance"
(→SyntaxCorrection Parent Class) |
(→First Example Creation) |
||
Line 38: | Line 38: | ||
</tabber> | </tabber> | ||
− | + | The above Animal class can act as a parent for all Animals you may create down the line, for example: | |
− | + | ||
− | + | <tabber> | |
− | + | C#= | |
− | + | <syntaxhighlight lang="csharp" line> | |
− | <syntaxhighlight lang="csharp"> | + | |
− | + | class Tiger : Animal { | |
− | + | public Tiger(name, age, gender, numOfLegs) : base(name, age, gender) { | |
− | + | // S.N. the 'base' after the : calls the base constructor of the Parent Animal class | |
− | + | this.numOfLegs = numOfLegs; | |
− | + | } | |
− | + | ||
− | + | private int _numOfLegs; | |
− | + | public int numOfLegs { get { return _numOfLegs; } set { _numOfLegs = value; } } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | </tabber> | ||
+ | |||
+ | The above Tiger class possesses all the same functions and code as the Animal class, but '''Builds''' on the Animal class by adding the instance property numOfLegs to the class and overrides the base constructor with a new constructor which calls the base constructor and then sets the new value restricted to the Tiger class (numOfLegs). The Good thing about this is that the Tiger Class can be given a new sibling by just creating a new class which also inherits from Animal, for example. | ||
For examples on how inheritance can be used to psuedo link different object instances to a given global class (allowing different, for example, Animals to all be contained in a single array despite being different types) view the examples on [[The Downsides of Arrays|Arrays]] at http://compsci.duckdns.org/mediawiki/index.php/Arrays. | For examples on how inheritance can be used to psuedo link different object instances to a given global class (allowing different, for example, Animals to all be contained in a single array despite being different types) view the examples on [[The Downsides of Arrays|Arrays]] at http://compsci.duckdns.org/mediawiki/index.php/Arrays. |
Revision as of 18:16, 17 December 2016
Definition
Inheritance is the process by which code applying to a given class can be functionally reused by another class with minimal effort. S.N. any Class which inherits from another class possesses an 'IS A' relationship (counter to a 'HAS A' relationship). Inheritance is best explained via abstraction, for example if we abstract a Ferret into it's many sub components, we find a Ferret 'IS A' Mamal which 'IS A' Animal. Class abstraction can have numerous degrees of seperation and allows the implementation of inheritance functionality, namely allowing any class in the inheritance chain to become a parent to a new class; for example if we add another mamal to the chain such as tiger, we can have this new class inherit all the functionality of the class Mamal and allow it to posess the exact same base to define itself as we did for Feret.
Inheritance Example
To display the qualities & additional functionality available to a program which implements inheritance, we will create a short program displaying Animals.
1 private enum EGender {
2 Male, female
3 // Enumeration containing
4 }; // possible gender values
5
6 class Animal {
7 public Animal(name, age, gender) {
8 this.name = name;
9 this.age = age;
10 this.gender = gender;
11 }
12
13 public void Walk() {
14 Console.WriteLine("{0} Is a {1} & is {2} Years Old",
15 this.name, this.gender, this.age)
16 }
17
18 private int _age;
19 private String _name;
20 private EGender _gender;
21
22 public int age { get { return _age; } set { _age = value; } }
23 public String name { get { return _name; } set { _name = value; } }
24 public EGender gender { get { return _gender; } set { _gender = value; } }
25 }
The above Animal class can act as a parent for all Animals you may create down the line, for example:
1 class Tiger : Animal {
2 public Tiger(name, age, gender, numOfLegs) : base(name, age, gender) {
3 // S.N. the 'base' after the : calls the base constructor of the Parent Animal class
4 this.numOfLegs = numOfLegs;
5 }
6
7 private int _numOfLegs;
8 public int numOfLegs { get { return _numOfLegs; } set { _numOfLegs = value; } }
9 }
The above Tiger class possesses all the same functions and code as the Animal class, but Builds on the Animal class by adding the instance property numOfLegs to the class and overrides the base constructor with a new constructor which calls the base constructor and then sets the new value restricted to the Tiger class (numOfLegs). The Good thing about this is that the Tiger Class can be given a new sibling by just creating a new class which also inherits from Animal, for example.
For examples on how inheritance can be used to psuedo link different object instances to a given global class (allowing different, for example, Animals to all be contained in a single array despite being different types) view the examples on Arrays at http://compsci.duckdns.org/mediawiki/index.php/Arrays.