Chapter 10 Introduction to Inheritance Understanding Inheritance Inheritance

  • Slides: 29
Download presentation
Chapter 10: Introduction to Inheritance

Chapter 10: Introduction to Inheritance

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

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 Microsoft Visual C# 2012, Fifth Edition 2

Microsoft Visual C# 2012, Fifth Edition 3

Microsoft Visual C# 2012, Fifth Edition 3

Understanding Inheritance (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 4

Understanding Inheritance (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 4

Understanding Inheritance Terminology • Base class – A class that is used as a

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 Microsoft Visual C# 2012, Fifth Edition 5

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

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 Microsoft Visual C# 2012, Fifth Edition 6

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 7

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 7

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 8

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 8

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 9

Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 9

Using the protected Access Specifier • Any derived class inherits all the data and

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 Microsoft Visual C# 2012, Fifth Edition 10

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 11

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 11

Microsoft Visual C# 2012, Fifth Edition 12

Microsoft Visual C# 2012, Fifth Edition 12

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 13

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 13

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 14

Using the protected Access Specifier (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 14

Creating and Using Abstract Classes • Abstract class – One from which you cannot

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 Microsoft Visual C# 2012, Fifth Edition 15

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 16

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 16

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 17

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 17

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 18

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 18

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 19

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 19

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 20

Creating and Using Abstract Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 20

Creating and Using Interfaces • Multiple inheritance – The ability to inherit from 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 Microsoft Visual C# 2012, Fifth Edition 21

Creating and Using Interfaces (cont’d. ) • In an abstract class, not all methods

Creating and Using Interfaces (cont’d. ) • In an abstract class, not all methods need to be abstract • In an interface, all methods are abstract Microsoft Visual C# 2012, Fifth Edition 22

(continues) Microsoft Visual C# 2012, Fifth Edition 23

(continues) Microsoft Visual C# 2012, Fifth Edition 23

Microsoft Visual C# 2012, Fifth Edition 24

Microsoft Visual C# 2012, Fifth Edition 24

Creating and Using Interfaces (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 25

Creating and Using Interfaces (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 25

Creating and Using Interfaces (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 26

Creating and Using Interfaces (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 26

Creating and Using Interfaces (cont’d. ) • You cannot instantiate concrete objects from either

Creating and Using Interfaces (cont’d. ) • 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 Microsoft Visual C# 2012, Fifth Edition 27

Recognizing Inheritance in GUI Applications and Recapping the Benefits of Inheritance • Every Form

Recognizing Inheritance in GUI Applications and Recapping the Benefits of Inheritance • Every Form you create using Visual Studio’s IDE is a descendent of the Form class Microsoft Visual C# 2012, Fifth Edition 28

Microsoft Visual C# 2012, Fifth Edition 29

Microsoft Visual C# 2012, Fifth Edition 29