Difference between revisions of "Inheritance"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "== Inheritance == <syntaxhighlight lang="csharp"> Class Animal{ private int age; private string name; private enum gender{ Male, female }; public ...")
 
(Inheritance)
Line 8: Line 8:
 
       female
 
       female
 
     };
 
     };
   public Animal(){
+
   public Animal(){ //Class Constructor
 
       //some code
 
       //some code
 
   }
 
   }
Line 26: Line 26:
 
       Other
 
       Other
 
   };
 
   };
 +
  public Mammal(){ //Class Constructor
 +
      //some code
 +
  }
 
   public override void Walk(){
 
   public override void Walk(){
 
       //Some different code
 
       //Some different code

Revision as of 17:11, 15 December 2016

Inheritance

 
Class Animal{
   private int age;
   private string name;
   private enum gender{
      Male,
      female
    };
   public Animal(){ //Class Constructor
      //some code
   }
   public void Walk(){
      //some more code
   }
}
  • Basic example class of a generic animal.
 
Class Mammal: Animal{
   private enum hairColour{
      White,
      Black,
      Brown,
      Other
   };
   public Mammal(){ //Class Constructor
      //some code
   }
   public override void Walk(){
      //Some different code
   }
}
  • 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.