───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───
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,
Applewould be a subclass of the superclassFruitApplewould have the behavior ofFruit, along withApplespecific 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
Addmethod 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
Appleis a specializedFruit - A
Studentis 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
Shapemay not be able to calculate its area, but aRectanglecan
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;
}───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───