Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Polymorphism
MIS 21 – Introduction to Applications Development
Outline
➢ Review of inheritance
➢ Inheritance in C#
➢ Access modifiers
➢ Overriding methods
➢ Partial types
➢ Sealed classes
➢ Interfaces
➢ Abstract classes
Review of inheritance
➢ Inheritance:an OOP language feature that
allows for defining a class in terms of
another class
➢ A relationship between classes
➢ We say that a derived class (or subclass)
inherits from a base class (or superclass)
➢ Promotes the reusability of existing code
Review of inheritance
➢ Inheritance relationships produce a class hierarchy
➢ All types are ultimately derived from System.Object
System.Object
Shape
Circle Quadrilateral
Rectangle Trapezoid
Square
Inheritance in C#
➢ Suppose you had a BankAccount class:
public ParentClass(string n) {
public ChildClass(string m)
: base(m) {
}
Inheritance in C#
public class BankAccount {
private string owner;
private double balance;
public BankAccount(string owner, int balance) {
this.owner = owner;
this.balance = balance;
}
public BankAccount(string owner) : this(owner, 0) {
}
}
}
Abstract classes
➢ An abstract class cannot be instantiated
➢ Abstract classes are intended to be used as a base class
➢ Use abstract keyword
➢ May contain abstract or non-abstract methods
➢ Abstract methods contain no implementation (but the non-
abstract methods do)
➢ Abstract methods are virtual
➢ Abstract methods can be overridden in derived classes (use
override)
➢ Differences:
➢ Interfaces cannot contain any implementation
➢ Interfaces cannot declare non-public members
➢ Interfaces cannot extend non-interfaces