Chapter 10 Introduction to Inheritance Understanding Inheritance Inheritance
- Slides: 29
Chapter 10: Introduction to Inheritance
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
Understanding Inheritance (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 4
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 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 8
Extending Classes (cont’d. ) Microsoft Visual C# 2012, Fifth Edition 9
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
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 14
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 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 19
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 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 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
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 26
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 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
- Introduction to inheritance
- Section 2 complex patterns of inheritance
- Chapter 16 the molecular basis of inheritance
- Chapter 15: the chromosomal basis of inheritance
- Chapter 11 complex inheritance and human heredity test
- Chapter 9 patterns of inheritance
- Chapter 16: the molecular basis of inheritance
- Chapter 15: the chromosomal basis of inheritance
- The chromosomal basis of inheritance chapter 15
- Chapter 15 the chromosomal basis of inheritance
- Chapter 11 section 1 basic patterns of human inheritance
- Chapter 11 section 1 basic patterns of human inheritance
- Chapter 13-the molecular basis of inheritance
- Chapter 9 patterns of inheritance
- Chapter 15: the chromosomal basis of inheritance
- Chapter 9 lesson 3 understanding violence
- Understanding the scriptures chapter 24 study questions
- Chapter 6 understanding the management process
- Understanding the management process
- Chapter 5 section 1 understanding supply
- Sarophytes
- Understanding the scriptures chapter 22 study questions
- Understanding the scriptures chapter 20 study questions
- Chapter 16 section 1 water in the air answer key
- Chapter 8 understanding populations
- Chapter 4 lesson 1 understanding stress
- Chapter 4 section 1 understanding demand
- Chapter 23 understanding income and taxes
- Ap stats chapter 10 understanding randomness
- The ability to locate interpret and apply information