-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
184 lines (153 loc) · 5.69 KB
/
Program.cs
File metadata and controls
184 lines (153 loc) · 5.69 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
namespace _9Inheritance
{
//INHERITANCE
//Inheritance allows a class to "inherit" or use the properties, methods,
//and behavior defined by another class.
//We generally think of inheritance as representing an "is a" or "is a kind of"
//relationship. For example, a Dog is an Animal, a Toyota Corolla is a Car,
//and a Dresser is a kind of Furniture.
public class Insect { /*...*/ }
public class Bee : Insect { /*...*/ }
public class Car { /*...*/ }
public class ToyotaCorolla : Car { /*...*/ }
public class Furniture { /*...*/ }
public class Dresser : Furniture { /*...*/ }
//You can have multiple classes inherit from a single base class.
//In the below classes, both Dog and Wolf inherit from the Animal class.
public class Animal //Base class
{
public string SpeciesName { get; set; }
public bool IsDomesticated { get; set; }
public virtual void MakeSound()
{
Console.WriteLine("Basic Animal Sound");
}
}
public class Dog : Animal //Derived class
{
public string BreedName { get; set; }
public Dog(string breedName)
{
SpeciesName = "Canis familiaris";
IsDomesticated = true;
BreedName = breedName;
}
public override void MakeSound()
{
Console.WriteLine("Bark!");
}
}
public class Wolf : Animal //Derived class
{
public Wolf()
{
SpeciesName = "Canis lupus";
IsDomesticated = false;
}
public override void MakeSound()
{
Console.WriteLine("Awooooooooo!");
}
}
//ACCESS MODIFIERS
//As we learned in Part 7 - Classes, a C# class may have an access modifier (e.g. public, private, etc.)
//Properties marked as private are not accessible in derived classes.
public class Animal2
{
private string SpeciesName;
protected bool IsDomesticated { get; set; }
public bool IsExtinct { get; set; }
}
public class Dodo : Animal2
{
//We cannot access SpeciesName here, because it is private.
//However, we CAN access IsDomesticated, because it is protected.
//Protected properties in base classes can be accessed in derived classes.
public Dodo()
{
IsDomesticated = false;
IsExtinct = true;
}
}
//OVERRIDING MEMBERS
public class Animal3
{
public virtual void MakeSound() { /*...*/ }
}
public class Tiger : Animal3
{
public override void MakeSound()
{
Console.WriteLine("Roar!");
}
}
public class Hobbes : Animal3
{
public override void MakeSound()
{
Console.WriteLine("Denial springs eternal.");
}
}
//BASE CONSTRUCTORS
//If the base class has a constructor method,
//it can be called from the derived class's constructor
//using the base keyword.
public class Animal4
{
public string SpeciesName { get; set; }
public Animal4(string speciesName)
{
Console.WriteLine("The species name is " + speciesName);
SpeciesName = speciesName;
}
}
public class BlueWhale : Animal4
{
public BlueWhale() : base("Balaenoptera musculus") { /*...*/ }
}
//IMPLICIT INHERITANCE
//Remember from Part 1 of this series that all C# objects must inherit from
//the common base class System.Object. This means that they can also use
//all methods defined on that class.
public class Vegetable { } //We create an instance of Vegetable in the Main method below.
//NO MULTIPLE INHERITANCE
//C# does not permit a single class to inherit from multiple other classes.
//However, there are other ways to share behavior, as we will see in Part 10.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-----------------Basic Inheritance--------------------");
Dog dog = new Dog("Labrador Retriever");
dog.MakeSound();
Wolf wolf = new Wolf();
wolf.MakeSound();
//ACCESS MODIFIERS
Console.WriteLine("-----------------Access Modifiers--------------------");
//When we instantiate a Dodo, we cannot access the property SpeciesName, because it is private.
//We also cannot access the property IsDomesticated, because it is protected.
//We can only access IsExtinct, because that property is public.
var dodo = new Dodo();
dodo.IsExtinct = true;
Console.WriteLine(dodo.IsExtinct);
//OVERRIDING MEMBERS
Console.WriteLine("------------------Overriding Members---------------------");
Tiger tiger = new Tiger();
tiger.MakeSound();
Hobbes hobbes = new Hobbes();
hobbes.MakeSound();
//BASE CONSTRUCTORS
Console.WriteLine("------------------Base Constructors----------------------");
BlueWhale blueWhale = new BlueWhale();
//IMPLICIT INHERITANCE
Console.WriteLine("-----------------Implicit Inheritance--------------------");
//When we create a Vegetable, remember that Vegetable has no members.
//And yet, it can still use all the members defined on System.Object.
Vegetable myVegetable = new Vegetable();
var type = myVegetable.GetType(); //Method implemented in System.Object
Console.WriteLine(type);
Console.WriteLine(myVegetable.GetHashCode()); //GetHashCode implemented in System.Object
}
}
}