Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Chương 4: Understanding Object-Oriented Programming Inheritence pps
Nội dung xem thử
Mô tả chi tiết
Chapter 4. Understanding
Object-Oriented
Programming: Inheritance
Hoang Anh Viet
Hanoi University of Technology
1
Microsoft
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
Microsoft
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
Microsoft
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
Microsoft
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
Microsoft
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
Microsoft
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
Microsoft
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