Chapter 07

advertisement
Chapter Seven
Introduction to
Inheritance
1
Objectives
•
•
•
•
•
Learn about the concept of inheritance
Learn inheritance terminology
How to extend classes
How to use the protected access specifier
How to override superclass methods
2
Objectives
• How to access superclass methods from a
subclass
• How a subclass object “is an” instance of the
superclass
• About the Object class
• How to work with superclasses that have
constructors
• How to work with superclass constructors that
require arguments
3
Objectives
• How to create and use abstract classes
• How to create and use interfaces
• Learn the benefits of inheritance
4
Understanding the Concept of Inheritance
• Inheritance is the principle that states you can apply
your knowledge of a general category to more specific
objects
• When you create a class by making it inherit from
another class, you are provided with data fields and
methods automatically
• There are many advantages to using inheritance
5
Understanding the Concept of Inheritance
• An Employee class
6
Understanding the Concept of Inheritance
• The ability to use inheritance makes programs easier to
write, less error-prone, and easier to understand
7
Understanding Inheritance Terminology
• A class that is used as a basis for inheritance is called a
base class
• When you create a class that inherits from a base class,
it is a derived class or extended class
• You can use the terms superclass and subclass as
synonyms for base class and derived class
• You usually can distinguish base classes from their
subclasses by size
8
Understanding Inheritance Terminology
• A derived class can be further extended
• Inheritance is transitive; that means a child inherits all
the members of all its ancestors
• When you create your own transitive inheritance chains,
you want to place fields and methods at their most
general level
9
Extending Classes
• When you create a class that is an extension or child of
another class, you use a single colon between the
derived class name and its base class name
• Inheritance works in one direction
10
Using the protected Access Specifier
• Occasionally, you want to allow a subclass to access
parent class data, while still abiding by the principle of
information hiding
• The keyword protected provides you with an
intermediate level of security
• A protected data field or method can be used within its
own class or in any classes extended from that class, but
it cannot be used by “outside” classes
11
Using the protected Access Specifier
• Declaring empSal as protected and accessing it within
CommissionEmployee
12
Overriding Superclass Methods
• When you create a subclass by extending an existing
class, the new subclass contains data and methods that
were defined in the original superclass
• Sometimes the superclass data fields and methods are
not appropriate for the subclass objects
• Using the same method name to indicate different
implementations is called polymorphism
13
Overriding Superclass Methods
• In the above child class, the SetCredits() method is
declared as new because it has the same name and
argument list as a method in its parent class—it
overrides its counterpart
14
Overriding Superclass Methods
• DemoStudents program and Output
15
Accessing Superclass Methods from a
Subclass
• A subclass can contain a method with the same name
and arguments as a method in its parent class
• When you want to use the parent class method within a
subclass use the base keyword to access the parent
class method
• A method that calls itself is a recursive method
16
Understanding a Subclass Object “is an”
Instance of the Superclass
• Every subclass object “is a” specific instance of both the
subclass and the superclass
• You can assign a subclass object to an object of any of
its superclass types. When you do so, C# makes an
implicit conversion from subclass to superclass
• C# also makes implicit conversions when casting one
data type to another
17
Using the Object Class
• Every class you create in C# derives from a single class
named System.Object
• The object (or Object) class type in the System
namespace is the ultimate base class for all other types
• Every class descends from Object
18
Using the Object Class
• The Four public Instance Methods of the Object class
19
Using the Object Class
•
•
•
•
The GetType() method returns an object’s type, or class
Object class methods are usually overridden
The ToString() method can be useful for debugging
The Object class’s Equals() method returns true if two
Objects have the same memory address
20
Working with Superclasses that have
Constructors
• When you instantiate an object that is a member of a
subclass, you actually call two constructors
• When you create any subclass object, the base class
constructor must execute first
21
Using Superclass Constructors that Require
Arguments
• When you use a class as a superclass, and the class
has a constructor that requires arguments, then you
must make sure that any subclasses provide the
superclass constructor with the proper arguments
• The format of the statement that calls a superclass
constructor is base(list of arguments)
• C# does not allow you to call the superclass constructor
by name, it must be called using the base keyword
22
Creating and Using Abstract Classes
• An abstract class is one from which you cannot create
any concrete objects, but from which you can inherit
• An abstract method has no method statements; any
class derived from a class containing an abstract method
must override the abstract method by providing a body
for it
• When you create an abstract method, you provide the
keyword abstract and the intended method type, name,
and argument
• When you create a subclass that inherits an abstract
method from a parent, you must use the override
keyword
23
Creating and Using Abstract Classes
• Animal class
24
Creating and Using Abstract Classes
• Dog and Cat classes
25
Creating and Using Abstract Classes
• DemoAnimals program and Output
26
Creating and Using Interfaces
• The ability to inherit from more than one class is called
multiple inheritance
• Multiple inheritance creates many complicated problems,
and as a result is prohibited in C#
• C# does provide an alternative to multiple inheritance in
the form of interfaces
27
Recapping the Benefits of Using Inheritance
• The benefits of inheritance are as follows:
– Subclass creators save development time because much of the
code that is needed for the class has already been written
– Subclass creators save testing time
– Programmers who create or use new subclasses already
understand how the superclass works, so the time it takes to
learn the new class feature is reduced
– The superclass maintains its integrity
28
Chapter Summary
• Inheritance is the principle that you can apply your
knowledge of a general category to more specific objects
• A class that is used as a basis for inheritance is called a
base class
• When you create a class that is an extension or child of
another class, you use a single colon between derived
class name and its base class name
• If you could use private data outside of its class, the
principle of information hiding would be destroyed
29
Chapter Summary
• You can declare a child class method with the same
name and argument list as a method within its parent
class
• When a subclass overrides a parent class method and
you want to use the parent class version, you can use
the keyword base to access the parent class method
• Every subclass object “is a” specific instance of both the
subclass and the superclass
• Every class you create in C# derives from a single class
named System.Object
30
Chapter Summary
• When you instantiate an object that is a member of a
subclass, you actually call two constructors
• When you use a class as a superclass, and the class
has a constructor that requires arguments, then within
the header of the subclass constructor you must provide
values for any arguments required by the base class
constructor
• An abstract class is one from which you cannot create
any concrete objects
• C# provides an alternative to multiple inheritance known
as the interface
31
Download