repl.it
@kironroy/

inheritance2

C#

better explanation

fork
loading
Files
  • main.cs
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;

// don't abuse inheritance make sure to follow "is a" rule

namespace InheritanceDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Phone> phones = new List<Phone>();  // below 3 are phones, but HotSpot is NOT

            phones.Add(new CellPhone());
            phones.Add(new SmartPhone());
            phones.Add(new LandLine());

            foreach (var phone in phones)
            {
                phone.PlaceCall();
            }

            Console.WriteLine();

            foreach (var phone in phones)
            {
                phone.EndCall();
            }
            
            Console.ReadLine();
        }
    }

    // Smartphone is a Cellphone.  A cellphone is a Phone
    // One grandfather (no multiple inheritance)
    
    public class HotSpot  // this does not inherit from anything breaks "is-a" rule 
    {

    }

    public class LandLine : Phone
    {

    }

    public class SmartPhone : CellPhone
    {
        public List<string> Apps { get; set; }
        public void ConnectToInternet()
        {

        }

    }

    public class CellPhone : Phone
    {
        public string Carrier { get; set; }
    }


    public class Phone
    {
        public void PlaceCall()
        {
            Console.WriteLine("I am a placing a call");
        }

        public void EndCall()
        {
            Console.WriteLine("I am a ending a call");

        }

    }

    public class WalkieTakie // this is not a phone!
    {

    }
}