Você está na página 1de 13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

Week 5: Inheritance
Topics
Polymorphism Interfaces Delegates Operator Overloading

Concept review in Exercise 6


C# base class and derived class - Polymorphism - Type of class Abstract Interfaces Delegates Sealed Assign C# different types of class - Base and derived class Class Base{ ...} Class Derived : Base { } - Abstract class Abstract BaseClass{ } Class DerivedClass { override } - Interfaces class Private Class derivedClass:baseClass , InterfaceX , InterfaceY {...........} - Sealed Sealed Class Sealed_Class{ .}; Operator Overloading: Using operator +,-,*,/,% with Object.

1/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

PART I: C # Concept of Inheritance


Exercise 1
What is an abstract method?

C# supports ____ types of Inheritance mechanisms. a) 1 b) 2 c) 3 d)4 They are: 1) _______________________________ 2) _______________________________

Exercise 2
Fill in the blanks in each of the following statements: 1) __________________ is a form of software reusability in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. 2) A base classs __________________ members can be accessed only in the base-class definition or in derived-class definitions. 3) In a(n) __________________ relationship, an object of a derived class also can be treated as an object of its base class. 4) In a(n) _________________ relationship, a class object has one or more references to objects of other classes as members. 5) A class exists in a(n) __________________ relationship with its derived classes. 6) A base classs __________________ members are accessible anywhere that the program has a reference to that base class or to one of its derived classes. 7) A base classs protected access members have a level of protection between those of public and __________________ access. 8) A base classs __________________ members can be accessed only in the same assembly. 9) When an object of a derived class is instantiated, the base classs __________________ is called implicitly or explicitly to do any necessary initialization of the base-class instance variables in the derived-class object. 10) Derived-class constructors can call base-class constructors via the __________________ reference.

2/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

Exercise 3
State whether each of the following is true or false. If false, explain why. 1) It is possible to treat base-class objects and derived-class objects similarly.

2) Base-class constructors are not inherited by derived classes

3) A has-a relationship is implemented via inheritance.

. 4) All methods, by default, can be overridden.

5) Method ToString of class System.Object is declared as virtual.

3/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

6) When a derived class redefines a base-class method using the same signature, the derived class is said to overload that base-class method.

7) A Car class has an is a relationship with its SteeringWheel and Brakes.

8) Inheritance encourages the reuse of proven high-quality software.

4/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

Exercise 4
1) Some programmers prefer not to use protected access because it breaks the encapsulation of the base class. Discuss the relative merits of using protected access vs. insisting on using private access in base classes.

2) Many programs written with inheritance could be written with composition instead, and vice versa. Which approach is more natural, why?

5/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

PART II: Design


Exercise 5
Write down all the shapes you can think of both two-dimensional and three-dimensional and form those shapes into a shape hierarchy. Your hierarchy should have base class Shape from which class TwoDimensionalShape and class ThreeDimensionalShape are derived. Once you have developed the hierarchy, define each of the classes in the hierarchy.

6/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

PART III: Format


Exercise 6

public interface IAmABadInterface { int myInt = 0; void MyMethod() { Console.WriteLine("Eek!"); } }

Compile this program. Explain why this program cannot run?

7/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

Exercise 7

How to use abstract class:

abstract public class Employee { ...} Employee X = new Employee();

Compile this program. Explain why this program cannot run?

8/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

PART III: Programming


Exercise 9

Initial classes
1. Modify the person class to contain an enumeration of the marital status called MaritalStatus containing values for (Single, Married, Divorced or Widowed). 2. Modify the CanMarry() method and the constructor.

9/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

Exercise 10
In this exercise, you are going to write an IStudent interface and a PartTimeStudent class that implements it. These may be used in a scenario where employees are allowed to register as part time students; as such one can be an employee and a student at the same time. 1. Write an interface, IStudent 2. Add two simple properties as described Property Type Description LibCardNumber string Library card number Year int Year of study

3. Add two methods


o o

void Register() void PostCourseWork(string work)

4. Write a class PartTimeStudent that inherits from Employee and implements the IStudent interface

5. Modify your program to allow Person, Employee or PartTimeStudent classes to be used

10/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

11/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

LAB 2: HOMEWORK
1 1. Write a program demonstrating delegate to changing lower characters into upper characters.

2. Following codes contain error, correct them and write a class Circle to implement this interfaces.
public interface IDimensions { long width; long height; double Area(); double Circumference(); int Side(); }

12/13

HaNoi University of Technology

Week 5: Inheritance Mastering C# 2008

13/13

Você também pode gostar