Difference between revisions of "Polymorphism"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Polymorphism is the term to describe inheritance trees of objects. A class can inherit subroutines from other classes, and can even overwrite them with its own iterations of t...")
 
(Example)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Polymorphism is the term to describe inheritance trees of objects. A class can inherit subroutines from other classes, and can even overwrite them with its own iterations of them. This is done using ":" when declaring a class.
+
If you use a class to define a sub class, the sub class will inherit all of the data items and methods defined within the base class. The sub class could however override the inherited version and replace it with the sub classes own version.
  
<h3>Syntax Example</h3>
+
<youtube>https://www.youtube.com/watch?v=_tYWKLIs5c4&index=5&list=PLCiOXwirraUAJPwNTW5gxfJK6Ej18fDP9</youtube>
[[File:inheritance example.png]]
+
 
 +
https://www.youtube.com/watch?v=_tYWKLIs5c4&index=5&list=PLCiOXwirraUAJPwNTW5gxfJK6Ej18fDP9
 +
 
 +
==Example==
 +
<syntaxhighlight lang="csharp">
 +
Class Mammal{
 +
 
 +
  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 virtual void Move(){ // virtual means it can be overridden
 +
      //Walk code
 +
  }
 +
 
 +
  public enum HairColour{
 +
      get{ return hairColour; }
 +
      set{ hairColour = value; }
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
However a Blue Whale is a sub class of Mammal, it will also need to have a Move method. However a Blue Whale obviously can't walk like most over mammals, so its move method will have code to make it swim. A bat is also a mammal, it can crawl (close enough to walk) but will also need to have code to allow it to fly. See the example subclasses below:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
Class Whale(Mammal){
 +
  public override void Move(){
 +
      //code to swim
 +
  }
 +
}
 +
 
 +
Class Bat(Mammal){
 +
  public override void Move(){
 +
      //Check if flying or crawling
 +
        //code to Fly
 +
      // else
 +
        //base.Move() // ie run the code from the mammal class
 +
  }
 +
}
 +
</syntaxhighlight>

Latest revision as of 08:35, 23 April 2021

If you use a class to define a sub class, the sub class will inherit all of the data items and methods defined within the base class. The sub class could however override the inherited version and replace it with the sub classes own version.

https://www.youtube.com/watch?v=_tYWKLIs5c4&index=5&list=PLCiOXwirraUAJPwNTW5gxfJK6Ej18fDP9

Example

 
Class Mammal{

   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 virtual void Move(){ // virtual means it can be overridden
      //Walk code
   }

   public enum HairColour{
      get{ return hairColour; }
      set{ hairColour = value; }
   }
}


However a Blue Whale is a sub class of Mammal, it will also need to have a Move method. However a Blue Whale obviously can't walk like most over mammals, so its move method will have code to make it swim. A bat is also a mammal, it can crawl (close enough to walk) but will also need to have code to allow it to fly. See the example subclasses below:

 
Class Whale(Mammal){
   public override void Move(){
      //code to swim
   }
}

Class Bat(Mammal){
   public override void Move(){
      //Check if flying or crawling
         //code to Fly
      // else
         //base.Move() // ie run the code from the mammal class
   }
}