Inheritance & Polymorphism
Specialization & Generalization The is-a relationship The dog is a mammal The cat is a mammal Dog and cat specialize mammal Mammal generalize dog and cat To extract the common parts In C#, the specialization relationship is typically implemented using inheritance.
Example Inheritance_demo Use : for inheritance Access modifiers Private, protected, public Override inherited methods new vs virtual-override Overload vs overwrite Observe the which method is called
Exercise 13 Please develop a Dog and a Cat class derives from mammal class and specialize them respectively The mammal class is given as the next slide
class Mammal { protected string speciesName = "Mammal"; public Mammal(string speciesName) this.speciesName = speciesName; } public void Sound() Console.WriteLine("The sound of {0} => hmm...hmm...", speciesName);
The root of all classes - Object Every thing is an object of Object in C# !!
Example (5-4) bad naming
Exercise 14 What will the message be if you don’t override the toString() in example 5-4? To understand why and when we need to do this.
Polymorphism The way you look at things Dog aDog = new Dog(); A dog is just a dog A dog is a mammal That dog is a hound Dog aDog = new Dog(); Mammal aMammal = (Mammal) aDog; ex13
Boxing & Un-Boxing The conversion between value types and reference types stack vs heap Boxing is implicit Un-Boxing is explicit
Boxing & Un-Boxing (5-5) implicit explicit
Interfaces An interface is a contract that guarantees to a client how a class will behave Doing nothing but putting a constraint on the implementing class Ex:
Example 8-1 Constructor Methods Property
Arrays An array is an indexed collection of objects, all of the same type Phone book (exercise 10) myFriend1, myFriend2, … , myFriend100 ? Expression :
Example (ArrayDemo) The phone book exercise can be improved further Array size static void Main(string[] args) { PersonalData[] myFriends = new PersonalData[2]; myFriends[0] = new PersonalData(); myFriends[0].Name = "Mandy Tsai"; myFriends[0].Phone = "0912345678"; myFriends[0].display(); PersonalData.Counter++; myFriends[1] = new PersonalData("Claire","0913245678"); myFriends[1].display(); } Array indexer
Exercise 15 Please try to declare and initialize an integer array from 0 to 100 myArray[0] = 0; myArray[1] = 1; … myArray[100] = 100; and prints them out 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 … 19
Some useful stuff Length property System.Array.Sort() myArray.Lngth System.Array.Sort() System.Array.Sort(myArray,0,101); System.Array.Reverse() System.Array.Reverse(myArray,0,101); The foreach loop foreach(int i in myArray)
Multi-dimensional array & ArrayList You can cast a multi-dimensional array simply by int[,] myArray = new int[6,8]; ArrayList is used to provide you an array with dynamic size Using System.Collections; Add(), Insert() Remove(), RemoveAt() Count (property)
Example (ex15_modified_2) ArrayList myArray = new ArrayList(); for(int i = 0; i < 101; i++) myArray.Add(i); { if(i != 0 && i%10 == 0) Console.WriteLine(); Console.Write(myArray[i]+"\t"); } Console.WriteLine("\n\n"); It is not necessary to provide the size of your array Add values into our array