Difference between revisions of "Inheritance"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Inheritance)
(Inheritance)
Line 1: Line 1:
 
== Inheritance ==
 
== Inheritance ==
 +
 +
*Basic example class of a generic animal.
 
<syntaxhighlight lang="csharp">  
 
<syntaxhighlight lang="csharp">  
 
Class Animal{
 
Class Animal{
Line 29: Line 31:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
*Basic example class of a generic animal.
+
*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.
 
<syntaxhighlight lang="csharp">  
 
<syntaxhighlight lang="csharp">  
 
Class Mammal: Animal{
 
Class Mammal: Animal{
Line 49: Line 55:
 
   }
 
   }
 
}
 
}
</syntaxhighlight>  
+
</syntaxhighlight>
*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.
 

Revision as of 17:25, 15 December 2016

Inheritance

  • Basic example class of a generic animal.
 
Class Animal{
   private int age;
   private string name;
   private enum gender{
      Male,
      female
    };
   public Animal(){ //Class Constructor
      //some code to run when a new instance of the class is created
   }
   public void Walk(){
      //some more code
   }
   public int Age{
      get{ return age; }
      set{ age = value; }
   }
   public string Name{
      get{ return name; }
      set{ name = value; }
   }
   public enum Gender{
      get{ return gender; }
      set{ gender = value; }
   }
}
  • 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.

 
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; }
   }
}