Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes...

25
Chapter 10: Introduction to Inheritance

Transcript of Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes...

Page 1: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Chapter 10:

Introduction to Inheritance

Page 2: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •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

2Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

3Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Understanding Inheritance (cont’d.)

4Microsoft Visual C# 2012, Fifth Edition

Page 5: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Extending Classes (cont’d.)

7Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Extending Classes (cont’d.)

8Microsoft Visual C# 2012, Fifth Edition

Page 9: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Extending Classes (cont’d.)

9Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Using the protectedAccess 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 Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Using the protectedAccess Specifier (cont’d.)

11Microsoft Visual C# 2012, Fifth Edition

Page 12: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

12Microsoft Visual C# 2012, Fifth Edition

Page 13: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Using the protectedAccess Specifier (cont’d.)

13Microsoft Visual C# 2012, Fifth Edition

Page 14: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Using the protectedAccess Specifier (cont’d.)

14Microsoft Visual C# 2012, Fifth Edition

Page 15: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Creating and Using Abstract Classes (cont’d.)

16Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Creating and Using Abstract Classes (cont’d.)

17Microsoft Visual C# 2012, Fifth Edition

Page 18: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

Creating and Using Abstract Classes (cont’d.)

18Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

19Microsoft Visual C# 2012, Fifth Edition

Creating and Using Abstract Classes (cont’d.)

Page 20: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 21: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 22: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

23Microsoft Visual C# 2012, Fifth Edition

(continues)

Page 23: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

• 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 24: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

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 25: Chapter 10: Introduction to Inheritanceturgaybilgin/2013-2014-bahar/SE... · Extending Classes •Use a single colon between the derived class name and its base class name •Inheritance

29Microsoft Visual C# 2012, Fifth Edition