Practical 6: Inheritance

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

Inheritance in C#

Inheritance allows to use the properties and methods of an existing class in a new class. The existing class is called the base class or parent class and the new class is called the derived class or child class. In C#, use the colon : for inheritance.

class Rectangle {
    public double Width { get; }
    public double Height { get; }
    public double Area { 
        get { return Width * Height; } 
    }
    public double Perimeter { 
        get { return 2 * (Width + Height); } 
    }
    public Rectangle(double width, double height) {
        Width = width;
        Height = height;
    }
}
class Square : Rectangle {
    public Square(int side) {
        Width = side;
        Height = side;
    }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Access Modifiers

Besides public and private, inheritance introduces another protected access modifier. A protected member is accessible within the class and any derived class.

class Base {
    public bool Hidden { get; private set; }
    public int Value { get; protected set; }
}
class Derived : Base {
    public void SetValue(int value) {
        Value = value; // OK
        Hidden = true; // Error
    }
}
class Program {
    static void Main() {
        Base b = new Base();
        Console.WriteLine(b.Value); // OK
        b.Value = 10; // Error
    }
}

In the example above, Value is settable only within the Base and Derived classes (protected set), but readable from any class (public get).

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Reusing Base Class Constructor

A derived class can reuse the constructor of its base class using the base keyword.

class Base {
    public int Value { get; private set; }
    public Base(int value) {
        Value = value;
    }
}
class Derived : Base {
    private int extra;
    public Derived(int value, int extra) : base(value) {
        this.extra = extra;
        Console.WriteLine(Value); // OK
    }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Abstract Classes

An abstract class is a class that cannot be instantiated and is used as a base class for other classes. An abstract class can have abstract methods, which must be implemented by derived classes.

abstract class Shape {
    // Abstract property
    public abstract double Area { get; }
    // Abstract method
    public abstract void Display();
}
// Error, must also implement Area and Display
class Circle : Shape {
    public double Radius { get; }
    public Circle(double radius) {
        Radius = radius;
    }
} 
Shape shape = new Shape(); // Error
Shape circle = new Circle(5); // OK (if Circle implements Area and Display)
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Class Diagrams

A class diagram shows the classes members and the relationships between classes.

alt text

alt text

CAB201 - Programming Principles
School of Computer Science, Faculty of Science