10266A_08.ppt
- Количество слайдов: 25
Module 8 Inheriting from Classes and Implementing Interfaces
Module Overview • Using Inheritance to Define New Reference Types • Defining and Implementing Interfaces • Defining Abstract Classes
Lesson 1: Using Inheritance to Define New Reference Types • What Is Inheritance? • The. NET Framework Inheritance Hierarchy • Overriding and Hiding Methods • Calling Methods and Constructors in a Base Class • Assigning and Referencing Classes in an Inheritance Hierarchy • Understanding Polymorphism • Defining Sealed Classes and Methods • Demonstration: Using Inheritance to Construct New Reference Types
What Is Inheritance? Inheritance enables you to define new types based on existing types: • For example, Manager and Manual. Worker classes might inherit from an Employee class • Fields and methods in the Employee class are inherited by Manager and Manual. Worker • Manager and Manual. Worker can define their own fields and behavior • Define accessible members as protected in the base class ü // Base class Employee { protected string emp. Num; protected string emp. Name; protected void Do. Work() {. . . } } // Inheriting classes class Manager : Employee { public void Do. Management. Work() {. . . } } class Manual. Worker : Employee { public void Do. Manual. Work() {. . . } } C# supports single inheritance only
The. NET Framework Inheritance Hierarchy All types inherit directly or indirectly from the System. Object class • No need to specify : Object in the class definition • Structs and enums inherit from Value. Type Object String Enum Value. Type Manager Employee Manual. Worker You cannot define your own inheritance hierarchy by using structs and enums
Overriding and Hiding Methods ü Overriding: Replace or extend functionality in a base class with semantically equivalent behavior (intentional) class Employee { protected void Do. Work() {. . . } } class Manager : Employee { public new void Do. Work() {. . . } } class Employee { protected virtual void Do. Work() {. . . } } class Manager : Employee { protected override void Do. Work() {. . . } } Hiding: Replace functionality in a base class with new behavior (possibly an error)
Calling Methods and Constructors in a Base Class Use the base keyword class Employee { protected string emp. Name; public Employee(string name) { this. emp. Name = name; } } class Manager : Employee { protected string emp. Grade; public Manager(string name, string grade) : base(name) { this. emp. Grade = grade; } } class Employee { protected virtual void Do. Work() {. . . } } class Manager : Employee { protected override void Do. Work() {. . . base. Do. Work(); } } Constructors automatically call the default constructor for the base type unless you specify otherwise
Assigning and Referencing Classes in an Inheritance Hierarchy C# type-checking prevents you from assigning references of one type to variables of a different Manager my. Manager = new Manager(…); Manual. Worker my. Worker = my. Manager; ü Manager my. Manager = new Manager(…); Employee my. Employee = my. Manager; ü Manager my. Manager. Again = my. Employee as Manager; type … … but you can assign a reference to a different type that is higher up an inheritance hierarchy Use the is and as operators to safely assign a reference to a type that is lower down in an inheritance hierarchy
Understanding Polymorphism In an inheritance hierarchy, the same statement can call different implementations of the same method Employee my. Employee; Manager my. Manager = new Manager(. . . ); Manual. Worker my. Worker = new Manual. Worker(. . . ); my. Employee = my. Manager; Console. Write. Line( my. Employee. Get. Type. Name()); my. Employee = my. Worker; Console. Write. Line( my. Employee. Get. Type. Name()); class Employee { public virtual string Get. Type. Name() { return "This is an Employee"; } } class Manager : Employee { public override string Get. Type. Name() { return "This is a Manager"; } } class Manual. Worker : Employee { // Does not override Get. Type. Name }
Defining Sealed Classes and Methods Object Custom Type Sealed Custom Type Seal a class to prevent it from being inherited Seal a method to prevent it from being overridden sealed class Manager : Employee {. . . } Value types are implicitly sealed class Manager : Employee { protected sealed override void Do. Work() }
Demonstration: Using Inheritance to Construct New Reference Types In this demonstration, you will see how to create a new class by inheriting from an existing class, and how to call methods from the base class in the child class
Lesson 2: Defining and Implementing Interfaces • What Is an Interface? • Creating and Implementing an Interface • Referencing an Object Through an Interface • Explicitly and Implicitly Implementing an Interface • Demonstration: Creating an Interface
What Is an Interface? An interface is: • A contract that specifies what methods must be exposed by an implementing class • Independent of the implementation IComparable : Compare. To(…) String Int 32 Employee BBB > AAA? 100 > 99 ? VP > Worker ? Alphanumeric Numeric Grade comparison
Creating and Implementing an Interface Use the interface keyword Do not specify access modifiers class Calculator : ICalculator { public double Add(){} public double Subtract(){} public double Multiply(){} public double Divide(){} } ü interface ICalculator { double Add(); double Subtract(); double Multiply(); double Divide(); } Add a colon followed by the interfaces being implemented Ensure that methods are publicly accessible C# classes can implement multiple interfaces
Referencing an Object Through an Interface You can define a class that implements an interface … … and then use that interface to reference an instance of that class You can also use interfaces as parameters for methods … … and use standard logic to test whether an object implements an interface, or whether an object that is referenced by an interface can be cast to a class Calculator : ICalculator {. . . } Calculator my. Calculator = new Calculator(); ICalculator i. My. Calculator = my. Calculator; public int Perform. Analysis(ICalculator) {. . . } Calculator calc = i. My. Calculator as Calculator; bool is. Calc = i. My. Calculator is Calculator; ICalculator i. Calc = my. Calculator as ICalculator; ;
Explicitly and Implicitly Implementing an Interface Implicit implementation: • Is only suitable for simple scenarios • Can lead to ambiguity class Calculator : ICalculator { double Add() { } • Means that all methods are visible double Subtract() { } when referencing an object by class Explicit implementation: } . . . • Is suitable for more complex scenarios • Prevents ambiguity • Is recommended when using class Calculator : ICalculator { double ICalculator. Add() { } interfaces double ICalculator. Subtract() { } • Must reference objects through the appropriate interface to invoke methods } . . .
Demonstration: Creating an Interface In this demonstration, you will see how to create an interface and implement it in a class
Lesson 3: Defining Abstract Classes • What Is an Abstract Class? • What Is an Abstract Method? • Demonstration: Creating an Abstract Class
What Is an Abstract Class? Abstract classes: • Contain common code, thereby reducing code duplication • Must be inherited • Cannot be instantiated • Can contain methods, fields, properties, and other members • Use the abstract keyword abstract class Salaried. Employee : Employee, ISalaried { void ISalaried. Pay. Salary() { Console. Write. Line("Pay salary: {0}", current. Salary); // Common code for paying salary. } int current. Salary; } class Manual. Worker : Salaried. Employee, ISalaried {. . . } class Manager : Salaried. Employee, ISalaried {. . . }
What Is an Abstract Method? Added to abstract classes abstract class Salaried. Employee : Employee, ISalaried. Employee { abstract void Pay. Bonus(); . . . } Defined with the abstract keyword Has no method body Abstract methods are useful when developing an abstract class that ü implements an interface or relies on a method where a default implementation is not appropriate ü Classes that inherit from a class with an abstract method must override that method, otherwise the code will not compile
Demonstration: Creating an Abstract Class In this demonstration, you will create an abstract class that has abstract methods, and then create a class that inherits from the abstract class
Lab: Inheriting from Classes and Implementing Interfaces • Exercise 1: Defining an Interface • Exercise 2: Implementing an Interface • Exercise 3: Creating an Abstract Class Logon information Virtual machine 10266 A-GEN-DEV User name Student Password Pa$$w 0 rd Estimated time: 60 minutes
Lab Scenario
Lab Review Questions • What steps are required to implement an interface? • How do you implement an abstract class?
Module Review and Takeaways • Review Questions • Best Practices