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...")
 
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.
+
Polymorphism is the term used to describe one method being able to take more than one type of input to process its code.
 
+
<syntaxhighlight lang="csharp" line>public static void Main(string[] args)
<h3>Syntax Example</h3>
+
  {
[[File:inheritance example.png]]
+
    int x = divide(15, 4);
 +
    double y = divide(2.5, 0.5);
 +
    Console.WriteLine(x);
 +
    Console.WriteLine(y);
 +
    Console.ReadLine();
 +
    /* writes:
 +
    * 3
 +
    * 5.0
 +
    */
 +
  }
 +
public static double divide(double a, double b)
 +
  {
 +
    return a / b;
 +
  }
 +
public static int divide(int a, int b)
 +
  {
 +
    return a / b;
 +
  }

Revision as of 20:04, 16 December 2016

Polymorphism is the term used to describe one method being able to take more than one type of input to process its code. <syntaxhighlight lang="csharp" line>public static void Main(string[] args)

 {
   int x = divide(15, 4);
   double y = divide(2.5, 0.5);
   Console.WriteLine(x);
   Console.WriteLine(y);
   Console.ReadLine();
   /* writes:
    * 3
    * 5.0
    */
 }

public static double divide(double a, double b)

 {
   return a / b;
 }

public static int divide(int a, int b)

 {
   return a / b;
 }