Difference between revisions of "Inheritance"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(URL Corrupt Fix)
(SyntaxCorrection Parent Class)
Line 4: Line 4:
  
 
== Inheritance Example ==
 
== Inheritance Example ==
To display the qualities &amp additional functionality available to a program which implements inheritance, we will create a short
+
To display the qualities &amp additional functionality available to a program which implements inheritance, we will create a short program displaying Animals.
<syntaxhighlight lang="csharp">  
+
 
Class Animal{
+
<tabber>
  private int age;
+
C#=
  private string name;
+
<syntaxhighlight lang="csharp" line>
  private enum gender{
+
private enum EGender {
      Male,
+
  Male, female
      female
+
  // Enumeration containing
    };
+
}; // possible gender values
  public Animal(){ //Class Constructor
+
 
      //some code to run when a new instance of the class is created
+
class Animal {
  }
+
    public Animal(name, age, gender) {
  public void Walk(){
+
        this.name = name;
      //some more code
+
        this.age = age;
  }
+
        this.gender = gender;
  public int Age{
+
    }
      get{ return age; }
+
 
      set{ age = value; }
+
    public void Walk() {
  }
+
        Console.WriteLine("{0} Is a {1} & is {2} Years Old",
  public string Name{
+
            this.name, this.gender, this.age)
      get{ return name; }
+
    }
      set{ name = value; }
+
 
  }
+
    private int _age;
  public enum Gender{
+
    private String _name;
      get{ return gender; }
+
    private EGender _gender;
      set{ gender = value; }
+
 
  }
+
    public int age { get { return _age; } set { _age = value; } }
 +
    public String name { get { return _name; } set { _name = value; } }
 +
    public EGender gender { get { return _gender; } set { _gender = value; } }
 
}
 
}
 +
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</tabber>
  
 
*A new Mammal class that inherits all aspect of the Animal class e.g name or gender but also adds its own unique
 
*A new Mammal class that inherits all aspect of the Animal class e.g name or gender but also adds its own unique
Line 60: Line 64:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
For examples on 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:05, 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 &amp 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 }

  • A new Mammal class that inherits all aspect of the Animal class e.g name or gender but also adds its own unique

data type, hairColour. This data is not accessible to any objects created from the Animal class.

  • Another thing inheritance allows you to do is override existing functions or data types. e.g.

in the Mammal class, the inherited function Walk() is being overridden meaning it can do something completely different to what it does in the Animal class. This is called polymorphism

 
Class Mammal: Animal{
   private enum hairColour{
      White,
      Black,
      Brown,
      Other
   };
   public Mammal(){ //Class Constructor
      //some code to run when a new instance of the class is created
   }
   public override void Walk(){
      //Some different code
   }
   public enum HairColour{
      get{ return hairColour; }
      set{ hairColour = value; }
   }
}

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.