Course: CSCI 1260
Code examples are in C#; the same concept applies to most* languages
Polymorphism
Subtype Polymorphism
- Inheritance
- A reference variable of the superclass can reference objects of its subclass
- For instance,
Apple
would be a subclass of the superclassFruit
Apple
would have the behavior ofFruit
, along withApple
specific behavior.- âAll apples are fruits, but not all apples are fruitsâ
Parametric Polymorphism
- Generics
- Being able to pass the data type as an argument to a class
- For instance,
List<string>
Ad-Hoc Polymorphism
- Overloading
- For instance, having an
Add
method that has two overloads, one for adding strings, and another for adding integers
public static int Add(int a, int b) => a + b;
public static string Add(string a, string b) => a + b;
Inheritance
- Generalization (parent/super/base) vs Specialization (child/sub/derived)
- An
Apple
is a specializedFruit
- A
Student
is a specializedPerson
- An
- Subclasses donât need to re-implement logic from the superclassâunless the methods are abstract
Code Example
public class Person(string name, DateOnly birthday)
{
public string Name { get; set; } = name;
public DateOnly Birthday { get; set; } = birthday;
public override string ToString() => $"{Name}, {Birthday}";
}
public class Student(string name, DateOnly birthday, string major, string id)
: Person(name, birthday)
{
// inherits `Name`, `Birthday`, `ToString()` from `Person`
public string Major { get; set; } = major;
public string Id { get; set; } = id;
}
UML Representation
- Represented with a hollow arrow pointing from the subclass to the superclass
classDiagram class Person { +name: string +birthday: DateOnly +Person(name: string, birthday: DateOnly) +ToString() string } class Student { +major: string +id: string +Student(name: string, birthday: DateOnly, major: string, id: string) +ToString() string } Person <|-- Student
Keywords
base
â used to call a superclassâs member from a subclassoverride
â required with a subclass method to override the superclassvirtual
â required within the superclass method so the method can be overridden
Abstract Classes and Methods
Most of the time, you should use interfaces instead
Abstract Classes
- Cannot instantiate abstract classes
- Only serve as a superclass for other classes
- Difficult to implement general cases, but easier to implement it in a more specific case in a derived class
Shape
may not be able to calculate its area, but aRectangle
can
public abstract class Shape
{
public abstract double Area();
}
public class Square(double length) : Shape
{
public double Area() => length * length
}
Abstract Methods
- Methods that do not have implementation
- Only have the signature of the method
public abstract double Area();
Interfaces
- A contract between the interface and classes that implement it with a âhas-aâ relationship
- The class must implement the logic within the interface; similar to abstract class
- A class can implement multiple interfaces
- Interfaces usually start with
I
(in C#)
interface IEmployee
{
public string Id { get; set; }
public double Salary { get; set; }
}
public class Employee(string name, DateOnly birthday, string id, double salary)
: Person(name, birthday), IEmployee
{
public string Id { get; set; } = id;
public double Salary { get; set; } = salary;
}