repl.it
@kironroy/

abstractclasses2

C#

better explanations

fork
loading
Files
  • main.cs
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
using System;

// Abstract classes are awesome, but remember to to follow "is-a" rule.

namespace AbstractClassDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // what is an inventory item, it's an abstract thing, not like a book, wrench, baseball...
            // InventoryItem item = new InventoryItem();

            // Can intit a inventory item as a book, because book is object
            InventoryItem item = new Book();

            // cannot creat instance of abstract class directly, because it is an abstract idea 
            // InventoryItem item = new InventoryItem();
            // Vechicle vechicle = new Vechicle();

            Book tropicOfCancerBook = new Book();

            tropicOfCancerBook.ProductName = "Tropic of Cancer";

            Console.ReadLine();
        }
    }

    // abstract class is a base class
    // class that you build upon
    // not complete on its own
    //  abstract classes can be intiantied

    public abstract class InventoryItem // abstract item
    {
        public string ProductName { get; set; }
        public string QuantityOnHand { get; set; }
    }

    // we know what a book is
    public class Book : InventoryItem
    {
        
    }


    // "Give me a vechicle" doesn't really make sense, what kind of vechicle?
    // vechicle class is abstract

    // *** abstract class is NOT THE same as interface
    // *** you can put methods inside your abstract class and use them in inherited classes
    // *** interfaces are just contracts
    public abstract class Vechicle  
    {
        public string VIN { get; set; }
        public string Manufacturer { get; set; }
        public string Model { get; set; }
        public int YearManufactured { get; set; }
    }

    public class Car : Vechicle
    {
        public int NumberOfWheels { get; set; } = 4;

    }
}