Introduction to C#         Lecture 4            FCISSummer training 2010, 1st year.
Contents      Inheritance      Calling base class constructors      The protected keyword      Dynamic binding; virtua...
Inheritance class Employee {         int salary;         public Employee( ) { salary = 100; }         public void Raise( )...
Inheritance class Test {      static void Main()      {              Teacher t = new Teacher();              t.Raise( ); /...
Calling base constructors      It is important for the derived classs constructor     to call the constructor of its pare...
Calling base constructors - 2 This code is correct, since Teachers default constructor calls   Employees default construct...
Calling base constructors - 3 This code is also correct, since Teachers Employees default   constructor is still called. ...
Calling base constructors - 4 But this code is incorrect, since the compiler cannot know what   parameters to give to Empl...
Calling base constructors - 4 Now the code is correct, since all of Teachers constructors give   the required parameters t...
Protected - 1class Employee{    int salary;    public Employee( ) { salary = 100; }    public void Raise( ) { salary += 50...
Protected - 2class Employee{    protected int salary;    public Employee( ) { salary = 100; }    public void Raise( ) { sa...
Overriding      A class like Teacher inherits all data members     and methods from its parent class.      But what if s...
Overriding     class Employee {        int salary;        public Employee() { salary = 100; }        public virtual void R...
Dynamic binding     class Employee {         int salary;         public Employee() { salary = 100; }         public virtua...
Object...      All classes inherit from object. So an instance     of any class can be assigned to a variable of     type...
Downcasts Employee e1, e2; Teacher t1, t2, t3; e1 = new Employee( );               // OK, same type Teacher t1 = new Teach...
Abstract classesclass Shape {      public virtual double GetArea() { ... }}                                               ...
Abstract classesabstract class Shape {      public abstract double GetArea();}class Square : Shape {    double side;    pu...
Abstract classes    Abstract classes represent common concepts    between many concrete classes.    A class that has one...
Extra: A simple dialog box...To end the session, well show how to create a simple    dialog box that reads a name from the...
Creating the dialog box...                              Value       Property       ObjectbtnOk   btnCancel             btn...
Creating the dialog box...In Form2:public string GetName( ){     return txtName.Text;}
Using a dialog box...In Form1        void btnGetName_Click(object sender, EventArgs e){    Form2 f = new Form2();    Dial...
Upcoming SlideShare
Loading in...5
×

C# Summer course - Lecture 4

649

Published on

C# summer training for FCIS, 2010. Lecture 4

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
649
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C# Summer course - Lecture 4

  1. 1. Introduction to C# Lecture 4 FCISSummer training 2010, 1st year.
  2. 2. Contents  Inheritance  Calling base class constructors  The protected keyword  Dynamic binding; virtual and override  Polymorphism and assignment compatibility  All classes inherit from object  The need for down casts  Abstract classes  Examples
  3. 3. Inheritance class Employee { int salary; public Employee( ) { salary = 100; } public void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; }  In this code, the class Teacher extends the employee class. This means: – Members of Employee like salary, Raise( ) are also members of teacher (Inheritance). – Objects of type teacher can be assigned to variables of type employee (Polymorphism).
  4. 4. Inheritance class Test { static void Main() { Teacher t = new Teacher(); t.Raise( ); // OK Employee e1 = new Employee(); // OK Employee e2 = new Teacher(); // OK } }
  5. 5. Calling base constructors  It is important for the derived classs constructor to call the constructor of its parent.  Calling the base constructor is done via the base keyword.  Some useful rules:  If (a) The derived constructor (e.g Teacher) doesnt call a base constructor, and (b) The base has a parameterless constructor, then the base parameterless constructor is called automatically.  Otherwise, the derived constructor must call the base constructor explicitly and give it parameters.
  6. 6. Calling base constructors - 2 This code is correct, since Teachers default constructor calls Employees default constructor class Employee { int salary; public Employee( ) { salary = 100; } public void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; }
  7. 7. Calling base constructors - 3 This code is also correct, since Teachers Employees default constructor is still called.  class Employee { int salary; public Employee( ) { salary = 100; } public void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; public Teacher() { } }
  8. 8. Calling base constructors - 4 But this code is incorrect, since the compiler cannot know what parameters to give to Employees only constructor!  class Employee { int salary; public Employee(int s) { salary = s; } public void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; public Teacher() { } // ??? }
  9. 9. Calling base constructors - 4 Now the code is correct, since all of Teachers constructors give the required parameters to the base classs constructors.  class Employee { int salary; public Employee(int s) { salary = s; } public void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; public Teacher() : base(100) { } // OK public Teacher(int _salary): base(_salary) {} // OK }
  10. 10. Protected - 1class Employee{ int salary; public Employee( ) { salary = 100; } public void Raise( ) { salary += 50;}}class Teacher : Employee{ string researchTopic; public Teacher( ) { salary = 500; // WRONG! salary is private }}
  11. 11. Protected - 2class Employee{ protected int salary; public Employee( ) { salary = 100; } public void Raise( ) { salary += 50;}}class Teacher : Employee{ string researchTopic; public Teacher( ) { salary = 500; // OK! salary is protected }}
  12. 12. Overriding  A class like Teacher inherits all data members and methods from its parent class.  But what if some of Teachers behavior is different from employee?  For example, what if Raise( ) should increment the salary by a different amount (say 51 instead of 50?).  In this case the derived class can override the method from its parent class.  But the parent class must allow overriding of this method by making it virtual.
  13. 13. Overriding class Employee { int salary; public Employee() { salary = 100; } public virtual void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; public Teacher() { } public override void Raise( ) { salary += 51; } }
  14. 14. Dynamic binding class Employee { int salary; public Employee() { salary = 100; } public virtual void Raise( ) { salary += 50;} } class Teacher : Employee { string researchTopic; public Teacher() { } public override void Raise( ) { salary += 51; } } class Test { static void Main( ) { Employee e = new Teacher( ); e.Raise( ); } }  Is e.salary now equal to 150 or 151?
  15. 15. Object...  All classes inherit from object. So an instance of any class can be assigned to a variable of type object, stored in an array of objects, passed to functions that take objects....etc  Also, object includes functions like ToString( ), which (a) Can be overriden and (b) Are used by standard .net code like Console.WriteLine and list boxes.  What about value types (which are not classes)? When you assign them to objects they are first copied into an object with a reference. This is called boxing.
  16. 16. Downcasts Employee e1, e2; Teacher t1, t2, t3; e1 = new Employee( ); // OK, same type Teacher t1 = new Teacher( ); e1 = t1; // OK, Teacher is a // subtype of Employee Teacher t2 = e2; // WRONG! Teacher t3 = (Teacher) e2; // OK for the compiler  The last line is a downcast. It means "assume e2 is really a reference to a Teacher object"  There will be a runtime check that this is the case, and an exception will be thrown if e2 isnt the correct type. Otherwise the program will go on normally.  Downcasts are sometimes needed, but are usually a sign of bad design; use polymorphism instead.
  17. 17. Abstract classesclass Shape { public virtual double GetArea() { ... }} ‫ماذا نضع‬class Square : Shape { ‫هنا؟؟؟‬ double side; public Square(double s) { side = s;} public override double GetArea() { return side*side;}}class Circle : Shape { double radius; public Circle(double r) { radius = r;} public override double GetArea() { return Math.PI * radius*radius;}}
  18. 18. Abstract classesabstract class Shape { public abstract double GetArea();}class Square : Shape { double side; public Square(double s) { side = s;} public override double GetArea() { return side*side;}}class Circle : Shape { double radius; public Circle(double r) { radius = r;} public override double GetArea() { return Math.PI * radius*radius;}}
  19. 19. Abstract classes Abstract classes represent common concepts between many concrete classes. A class that has one or more abstract functions must also be declared abstract. You cannot create instances of abstract classes (but youre free to create references whose types are abstract classes). If a class inherits from an abstract class, it can become concrete by overriding all the abstract methods with real methods. Now it can be instantiated.
  20. 20. Extra: A simple dialog box...To end the session, well show how to create a simple dialog box that reads a name from the user...
  21. 21. Creating the dialog box... Value Property ObjectbtnOk btnCancel btnOk AcceptButton Form btnCancel CancelButton Form txtName OK DialogResult btnOk Cancel DialogResult btnCancel
  22. 22. Creating the dialog box...In Form2:public string GetName( ){ return txtName.Text;}
  23. 23. Using a dialog box...In Form1 void btnGetName_Click(object sender, EventArgs e){ Form2 f = new Form2(); DialogResult r = f.ShowDialog( ); // Show modal if(r == DialogResult.OK) { string name = f.GetName( ); lblResult.Text = f; }}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×