You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
6.3 KiB
Markdown
303 lines
6.3 KiB
Markdown
# Inheritance & Abstract Classes
|
|
|
|

|
|
|
|
---
|
|
|
|
# Overview
|
|
|
|
Inheritance
|
|
|
|
Abstract Classes
|
|
|
|
Enums
|
|
|
|
# Inheritance
|
|
|
|
Classes can be made to inherit functionality of some other class
|
|
|
|
If class B inherits class A, all of the (public) functionality in class A is also available in class B
|
|
|
|
A is called the __base class__ (or parent class) and B is called the __derived class __ (or child class)
|
|
|
|
Use the " __:"__ symbol to make a class inherit from another
|
|
|
|
Try to understand the following example:
|
|
|
|
| class Animal |
|
|
| :-: |
|
|
| -Eat()-Sleep() |
|
|
|
|
| class Dog : Animal |
|
|
| :-: |
|
|
| -Bark() |
|
|
|
|
| class Cat : Animal |
|
|
| :-: |
|
|
| -Meow() |
|
|
|
|
Animal can eat
|
|
|
|
Animal can sleep
|
|
|
|
Dog can eat
|
|
|
|
Dog can sleep
|
|
|
|
Dog can bark
|
|
|
|
Cat can eat
|
|
|
|
Cat can sleep
|
|
|
|
Cat can meow
|
|
|
|
# Inheritance - Example
|
|
|
|
class Animal
|
|
|
|
{
|
|
|
|
public void Eat() { Console.WriteLine("Eating..."); }
|
|
|
|
public void Sleep() { Console.WriteLine("Sleeping..."); }
|
|
|
|
}
|
|
|
|
class Cat : Animal
|
|
|
|
{
|
|
|
|
public void Meow() { Console.WriteLine("Meow!"); }
|
|
|
|
}
|
|
|
|
class Dog : Animal
|
|
|
|
{
|
|
|
|
public void Bark() { Console.WriteLine("Bark!"); }
|
|
|
|
}
|
|
|
|
class Program
|
|
|
|
{
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
Dog pluto = new Dog();
|
|
|
|
pluto.Eat(); // Outputs "Eating..."
|
|
|
|
pluto.Bark(); // Outputs "Bark!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Inheritance
|
|
|
|
All the objects deriving from the same base class can be referenced with the base class name:
|
|
|
|
Animal whiskersAnimal = new Cat();
|
|
|
|
Animal plutoAnimal = new Dog();
|
|
|
|
However, only the methods of the base class are available for objects of the base class type:
|
|
|
|
plutoAnimal.Eat(); // Outputs "Eating..."
|
|
|
|
plutoAnimal.Bark(); // Error
|
|
|
|
An object of base class type can be __casted __ into the child class type:
|
|
|
|
Dog pluto = (Dog)plutoAnimal;
|
|
|
|
pluto.Bark(); // Outputs "Bark!"
|
|
|
|
# Abstract Classes
|
|
|
|
* In some cases you want the base class to be made __abstract__
|
|
* Objects of an abstract class cannot be instantiated
|
|
* For example, where would you need a generic Animal by itself?
|
|
* Make a class abstract with the abstract keyword:
|
|
* abstract class Animal
|
|
* {
|
|
* public void Eat() { Console.WriteLine("Eating..."); }
|
|
* public void Sleep() { Console.WriteLine("Sleeping..."); }
|
|
* }
|
|
* class Program
|
|
* {
|
|
* static void Main(string[] args)
|
|
* {
|
|
* Animal animal = new Animal(); // This will throw an error
|
|
* }
|
|
* }
|
|
|
|
Instead, the methods are accessible through a derived class:
|
|
|
|
abstract class Animal
|
|
|
|
{
|
|
|
|
public void Eat() { Console.WriteLine("Eating..."); }
|
|
|
|
public void Sleep() { Console.WriteLine("Sleeping..."); }
|
|
|
|
}
|
|
|
|
class Pig : Animal
|
|
|
|
{
|
|
|
|
public void Squeal() { Console.WriteLine("Squeee!"); }
|
|
|
|
}
|
|
|
|
class Program
|
|
|
|
{
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
Pig pig = new Pig();
|
|
|
|
pig.Sleep(); // Outputs "Sleeping..."
|
|
|
|
pig.Squeal(); // Outputs "Squeee!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
If you know you only need the functionality of the abstract class, instantiate the new class as a type of abstract class:
|
|
|
|
abstract class Animal{ public void Eat() { Console.WriteLine("Eating..."); } public void Sleep() { Console.WriteLine("Sleeping..."); }}class Pig : Animal{ public void Squeal() { Console.WriteLine("Squeee!"); }}class Program{ static void Main(string[] args) { Animal pig = new Pig(); pig.Sleep(); // Outputs "Sleeping..." pig.Squeal(); // This will throw an error }}
|
|
|
|
---
|
|
|
|
This is called abstraction, which is one of the key concepts of OOP
|
|
|
|
# Exercise 1
|
|
|
|
Create the classes Animal, Dog, Cat and Pig. Animal is the same as before (strings name and sound and the Greet() method). Dog, Cat and Pig inherit Animal
|
|
|
|
Give Dog, Cat and Pig some fields and/or methods specific to that animal
|
|
|
|
Create a few instances of Dog, Cat and Pig classes, and add them to a new list of Animals, named "allAnimals"
|
|
|
|
Loop through allAnimals with the foreach statement, and call the Greet() method of each animal
|
|
|
|
# Enum
|
|
|
|
__Enum __ (short for enumeration) is a data type for holding a set of constant (immutable) names
|
|
|
|
Enums can be useful when you have a number of items or states that are predefined, for example, weekdays
|
|
|
|
Create the enum type with the enum keyword:
|
|
|
|
enum Weekday{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
|
|
|
|
New instances of the enum can only be assigned one of the values within:
|
|
|
|
Weekday currentDay = Weekday.Monday;
|
|
|
|
# Enum - Example
|
|
|
|
In this example, enum is used to keep track of the state of the program:
|
|
|
|
enum ProgramState
|
|
|
|
{
|
|
|
|
Login,
|
|
|
|
Menu,
|
|
|
|
Exit
|
|
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
ProgramState currentState = ProgramState.Login;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
switch (currentState)
|
|
|
|
{
|
|
|
|
case ProgramState.Login:
|
|
|
|
// Switch current state to Menu after logging in
|
|
|
|
break;
|
|
|
|
case ProgramState.Menu:
|
|
|
|
// Switch current state to Exit if user exits
|
|
|
|
break;
|
|
|
|
case ProgramState.Exit:
|
|
|
|
// Exit the program with an exit message
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Going Further: Object Oriented Programming
|
|
|
|
* The instances that are created with the new -keyword are objects. This is literally what Object Orientation refers to: packing functionality into these reusable variables that are holding some data and can be passed around
|
|
* The key concepts of OOP are
|
|
* Encapsulation
|
|
* Inheritance
|
|
* Abstraction, and
|
|
* Polymorphism
|
|
|
|
# OOP: Encapsulation
|
|
|
|
Earlier we created classes which hold properties and methods, which are only accessible elsewhere _after _ instantiating an object of the class
|
|
|
|
All the functionality is encapsulated inside of the class instead of lying around in the codebase
|
|
|
|
All the functionality made available _only when it is needed _ by instantiating an object of the class
|
|
|
|
# OOP: Inheritance
|
|
|
|
As shown in the lecture slides, inheritance allows you to write some functionality once, and then create separate classes which all share that same functionality
|
|
|
|
This removes the need to write the same code inside every class
|
|
|
|
# OOP: Abstraction
|
|
|
|
When your class contains a lot of complicated functionality, it doesn't always make sense to reveal everything when the class is used
|
|
|
|
Instead, reveal only the parts that the user (you, a workmate, etc) actually need, with abstraction
|
|
|
|
Parable: The user of a microwave doesn't have to know about the complicated circuitry inside of the microwave. Only the buttons are revealed
|
|
|
|
# OOP: Polymorphism
|
|
|
|
This concept becomes more clear after we have covered interfaces
|
|
|
|
Polymorphism refers to multiple classes having the same method but with different functionality
|
|
|
|
This reduces the need for massive if-else and switch..case statements
|
|
|