Top Banner
Chapter 10: Introduction to Inheritance
29

Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Dec 18, 2015

Download

Documents

Hester Baker
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Chapter 10:Introduction to

Inheritance

Page 2: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Understanding Inheritance

• Inheritance– The principle that you can apply knowledge of a general

category to more specific objects

• Advantages of inheritance:– Saves time– Reduces the chance of errors– Makes it easier to understand the inherited class– Makes programs easier to write

2Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

3Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Understanding Inheritance (cont’d.)

4Microsoft Visual C# 2012, Fifth Edition

Page 5: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Understanding Inheritance Terminology

• Base class– A class that is used as a basis for inheritance– Also known as the superclass or parent class

• Derived class or extended class– A class that inherits from a base class– A derived class always “is a” case or an instance of the more

general base class– Also known as a subclass or child class

• Ancestors– A list of parent classes from which a child class is derived

• Inheritance is transitive– A child inherits all the members of all its ancestors 5Microsoft Visual C# 2012, Fifth Edition

Page 6: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Extending Classes

• Use a single colon between the derived class name and its base class name

• Inheritance works only in one direction– A child inherits from a parent

6Microsoft Visual C# 2012, Fifth Edition

Page 7: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Extending Classes (cont’d.)

7Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Extending Classes (cont’d.)

8Microsoft Visual C# 2012, Fifth Edition

Page 9: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Extending Classes (cont’d.)

9Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Using the protected Access Specifier

• Any derived class inherits all the data and methods of its base class – Including private data and methods– You cannot use or modify private data and methods

directly

• A protected data field or method:– Can be used within its own class or in any classes extended

from that class– Cannot be used by “outside” classes

• protected methods should be used sparingly

10Microsoft Visual C# 2012, Fifth Edition

Page 11: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Using the protected Access Specifier (cont’d.)

11Microsoft Visual C# 2012, Fifth Edition

Page 12: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

12Microsoft Visual C# 2012, Fifth Edition

Page 13: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Using the protected Access Specifier (cont’d.)

13Microsoft Visual C# 2012, Fifth Edition

Page 14: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Using the protected Access Specifier (cont’d.)

14Microsoft Visual C# 2012, Fifth Edition

Page 15: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Abstract Classes

• Abstract class– One from which you cannot create concrete objects, but

from which you can inherit– Use the keyword abstract when you declare an abstract

class– Usually contains abstract methods, although methods are

not required• Abstract method– Has no method statements– Derived classes must override it using the keyword override

15Microsoft Visual C# 2012, Fifth Edition

Page 16: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Abstract Classes (cont’d.)

16Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Abstract Classes (cont’d.)

17Microsoft Visual C# 2012, Fifth Edition

Page 18: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Abstract Classes (cont’d.)

18Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

19Microsoft Visual C# 2012, Fifth Edition

Creating and Using Abstract Classes (cont’d.)

Page 20: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

20Microsoft Visual C# 2012, Fifth Edition

Creating and Using Abstract Classes (cont’d.)

Page 21: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Interfaces

• Multiple inheritance– The ability to inherit from more than one class– A difficult concept

• Programmers encounter problems when they use it

– Prohibited in C#

• Interface– An alternative to multiple inheritance– A collection of methods that can be used by any class as

long as the class provides a definition to override the interface’s abstract definitions

21Microsoft Visual C# 2012, Fifth Edition

Page 22: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Creating and Using Interfaces (cont’d.)

• In an abstract class, not all methods need to be abstract

• In an interface, all methods are abstract

22Microsoft Visual C# 2012, Fifth Edition

Page 23: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

23Microsoft Visual C# 2012, Fifth Edition

(continues)

Page 24: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

24Microsoft Visual C# 2012, Fifth Edition

Page 25: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

25Microsoft Visual C# 2012, Fifth Edition

Creating and Using Interfaces (cont’d.)

Page 26: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

26Microsoft Visual C# 2012, Fifth Edition

Creating and Using Interfaces (cont’d.)

Page 27: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

• You cannot instantiate concrete objects from either abstract classes or interfaces

• A class can inherit from only one base class– However, it can implement any number of interfaces

• You create an interface when you want derived classes to override every method

27Microsoft Visual C# 2012, Fifth Edition

Creating and Using Interfaces (cont’d.)

Page 28: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

Recognizing Inheritance in GUI Applications andRecapping the Benefits of Inheritance

• Every Form you create using Visual Studio’s IDE is a descendent of the Form class

28Microsoft Visual C# 2012, Fifth Edition

Page 29: Chapter 10: Introduction to Inheritance. Understanding Inheritance Inheritance – The principle that you can apply knowledge of a general category to more.

29Microsoft Visual C# 2012, Fifth Edition