Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 88 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
88
Dung lượng
1,61 MB
Nội dung
Chapter 4. Understanding Object-Oriented Programming: Inheritance Hoang Anh Viet Vietha@it-hut.edu.vn Hanoi University of Technology 1 MicrosoftMicrosoft Objectives “This chapter introduced inheritancethe ability to create classes by absorbing an existing class's members and enhancing them with new capabilities. You learned the notions of base classes and derived classes and created a derived class that inherits members from a base class. The chapter introduced access modifier protected; derived class methods can access protected base class members. You learned how to access base class members with base. You also saw how constructors are used in inheritance hierarchies. Finally, you learned about Software Engineering with Inheritance.” Introduction Base Classes and Derived Classes protected and internal Members Relationship between Base Classes and Derived Classes Constructors and Destructors in Derived Classes Extension methods and Inheritance Software Engineering with Inheritance 2 MicrosoftMicrosoft Roadmap 4.1 Introduction 4.2 Base Classes and Derived Classes 4.3 protected and internal Members 4.4 Relationship between Base Classes and Derived Classes 4.5 Constructors and Destructors in Derived Classes 4.6 Extension methods and Inheritance 4.7 Software Engineering with Inheritance 3 MicrosoftMicrosoft 4.1 Introduction Defining the Pillars of OOP: Encapsulation: How does this language hide an object’s internal implementation details and preserve data integrity? Inheritance: How does this language promote code reuse? Polymorphism: How does this language let you treat related objects in a similar way? The Role of Inheritance: In essence, inheritance allows you to extend the behavior of a base (or parent) class by inheriting core functionality into the derived subclass (also called a child class) 4 MicrosoftMicrosoft 4.1 Introduction Inheritance: Classes are created by absorbing the methods and variables of an existing class It then adds its own methods to enhance its capabilities This class is called a derived class because it inherits methods and variables from a base class Objects of derived class are objects of base class, but not vice versa “Is a” relationship: derived class object can be treated as base class object (inheritance) “Has a” relationship: class object has object references as members (composition) A derived class can only access non-private base class members unless it inherits accessor funcitons 5 MicrosoftMicrosoft 4.1 Introduction Types Of Inheritance: what C# does and does not support ? Implementation vs. Interface Inheritance Implementation inheritance means that a type derives from a base type, taking all the basetype's member fields and functions. Interface inheritance means that a type inherits only the signatures of the functions, but does not inherit any implementations. Multiple Inheritance: C# does not support multiple implementation inheritance. It does, however, allow types to derive from multiple interfaces. Structs and Classes: structs do not support inheritance. 6 MicrosoftMicrosoft Roadmap 4.1 Introduction 4.2 Base Classes and Derived Classes 4.3 protected and internal Members 4.4 Relationship between Base Classes and Derived Classes 4.5 Constructors and Destructors in Derived Classes 4.6 Extension methods and Inheritance 4.7 Software Engineering with Inheritance 7 MicrosoftMicrosoft 4.2 Base Classes and Derived Classes An object often is an object of another class Every derived-class is an object of its base class Inheritance forms a tree-like heirarchy To specify class one is derived from class two class one : two Composition: Formed by “has a” relationships Constructors are not inherited 8 MicrosoftMicrosoft 4.2 Base Classes and Derived Classes 9 MicrosoftMicrosoft 4.2 Base Classes and Derived Classes Figure 4.2. UML class diagram showing an inheritance hierarchy for university CommunityMembers 10 . Chapter 4. Understanding Object-Oriented Programming: Inheritance Hoang Anh Viet Vietha@it-hut.edu.vn Hanoi University of