Thứ Bảy, 8 tháng 2, 2014

Tài liệu Module 10: Inheritance in C# docx

Module 10: Inheritance in C# 1


Overview
 Deriving Classes
 Implementing Methods
 Using Sealed Classes
 Using Interfaces
 Using Abstract Classes

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
Inheritance, in an object-oriented system, is the ability of an object to inherit
data and functionality from its parent object. Therefore, a child object can
substitute for the parent object. Also, by using inheritance, you can create new
classes from existing classes instead of starting at the beginning and creating
them new. You can then write new code to add the features required in the new
class. The parent class on which the new class is based is known as a base
class, and the child class is known as a derived class.
When you create a derived class, it is important to remember that a derived
class can substitute for the base class type. Therefore, inheritance is a type-
classification mechanism in addition to a code-reuse mechanism, and the former
is more important than the latter.
In this module, you will learn how to derive a class from a base class. You will
also learn how to implement methods in a derived class by defining them as
virtual methods in the base class and overriding or hiding them in the derived
class, as required. You will learn how to seal a class so that it cannot be derived
from. You will also learn how to implement interfaces and abstract classes,
which define the terms of a contract to which derived classes must adhere.
After completing this module, you will be able to:
 Derive a new class from a base class, and call members and constructors of
the base class from the derived class.
 Declare methods as virtual and override or hide them as required.
 Seal a class so that it cannot be derived from.
 Implement interfaces by using both the implicit as well as the explicit
methods.
 Describe the use of abstract classes and their implementation of interfaces.

Topic Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
about class inheritance in
C#.
2 Module 10: Inheritance in C#




 Deriving Classes
 Extending Base Classes
 Accessing Base Class Members
 Calling Base Class Constructors

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
You can only derive a class from a base class if the base class was designed to
enable inheritance. This is because objects must have the proper structure or
inheritance cannot work effectively. A base class that is designed for
inheritance should make this fact clear. If a new class is derived from a base
class that is not designed appropriately, then the base class might change at
some later time, and this would make the derived class inoperable.
After completing this lesson, you will be able to:
 Derive a new class from a base class.
 Access the members and constructors of the base class from the derived
class.

Topic Objective
To provide an overview of
the topics covered in this
section.
Lead-in
In this lesson, you will learn
how to derive a class from a
base class.
Module 10: Inheritance in C# 3


Extending Base Classes
 Syntax for deriving a class from a base class
 A derived class inherits most elements of its base class
 A derived class cannot be more accessible than its base
class
class Token
{

}
class CommentToken: Token
{

}
class Token
{

}
class CommentToken: Token
{

}
CommentToken
« concrete »
CommentToken
« concrete »
Token
« concrete »
Token
« concrete »
Derived class
Derived class
Derived class
Base class
Base class
Base class
Colon
Colon
Colon

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
Deriving a class from a base class is also known as extending the base class. A
C# class can extend at most one class.
Syntax for Deriving a Class
To specify that one class is derived from another, you use the following syntax:
class Derived: Base
{

}

The elements of this syntax are labeled on the slide. When you declare a
derived class, the base class is specified after a colon. The white space around
the colon is not significant. The recommended style for using this syntax is to
include no spaces before the colon and a single space after it.
Derived Class Inheritance
A derived class inherits everything from its base class except for the base class
constructors and destructors. Public members of the base class are implicitly
public members of the derived class. Private members of the base class, though
inherited by the derived class, are accessible only to the members of the base
class.
Topic Objective
To describe the procedure
for extending a base class.
Lead-in
The C# syntax for deriving
one class from another is
easy to understand.
4 Module 10: Inheritance in C#


Accessibility of a Derived Class
A derived class cannot be more accessible than its base class. For example, it is
not possible to derive a public class from a private class, as is shown in the
following code:
class Example
{
private class NestedBase { }
public class NestedDerived: NestedBase { } // Error
}

The C# syntax for deriving one class from another is also allowed in C++,
where it implicitly specifies a private inheritance relationship between the
derived and base classes. C# has no private inheritance; all inheritance is public.
Module 10: Inheritance in C# 5


Accessing Base Class Members
 Inherited protected members are implicitly protected in the derived
class
 Methods of a derived class can access only their inherited protected
members
 Protected access modifiers cannot be used in a struct
class Token
{ class Outside
protected string name; {
} void Fails(Token t)
class CommentToken: Token {
{
public string Name( ) t.name
{
return name; }
} }
}
class Token
{ class Outside
protected string name; {
} void Fails(Token t)
class CommentToken: Token {
{
public string Name( ) t.name
{
return name; }
} }
}





*****************************
ILLEGAL FOR NON-TRAINER USE******************************
The meaning of the protected access modifier depends on the relationship
between the class that has the modifier and the class that seeks to access the
members that use the modifier.
Members of a derived class can access all of the protected members of their
base class. To a derived class, the protected keyword behaves like the public
keyword. Hence, in the code fragment shown on the slide, the Name method of
CommentToken can access the string name, which is protected inside Token.
It is protected inside Token because CommentToken has specified Token as
its base class.
However, between two classes that are not related by a derived-class and base-
class relationship, protected members of one class act like private members for
the other class. Hence, in the other code fragment shown on the slide, the Fails
method of Outside cannot access the string name, which is protected inside
Token because Outside does not specify Token as its base class.
Topic Objective
To describe how protected
inheritance works.
Lead-in
Like other object-oriented
programming languages, C#
has protected as well as
public and private access
modifiers.
6 Module 10: Inheritance in C#


Inherited Protected Members
When a derived class inherits a protected member, that member is also
implicitly a protected member of the derived class. This means that protected
members are accessible to all directly and indirectly derived classes of the base
class. This is shown in the following example:
class Base
{
protected string name;
}

class Derived: Base
{
}

class FurtherDerived: Derived
{
void Compiles( )
{
Console.WriteLine(name); // Okay
}
}

Protected Members and Methods
Methods of a derived class can only access their own inherited protected
members. They cannot access the protected members of the base class through
references to the base class. For example, the following code will generate an
error:
class CommentToken: Token
{
void Fails(Token t)
{
Console.WriteLine(t.name); // Compile-time error
}
}


Many coding guidelines recommend keeping all data private and using
protected access only for methods.

Protected Members and structs
A struct does not support inheritance. Consequently, you cannot derive from a
struct, and, therefore, the protected access modifier cannot be used in a struct.
For example, the following code will generate an error:
struct Base
{
protected string name; // Compile-time error
}

Ti
p

Module 10: Inheritance in C# 7


Calling Base Class Constructors
 Constructor declarations must use the base keyword
 A private base class constructor cannot be accessed by a derived
class
 Use the base keyword to qualify identifier scope
class Token
{
protected Token(string name) { }

}
class CommentToken: Token
{
public CommentToken(string name) : base(name) { }

}
class Token
{
protected Token(string name) { }

}
class CommentToken: Token
{
public CommentToken(string name) : base(name) { }

}

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
To call a base class constructor from the derived class constructor, use the
keyword base. The syntax for this keyword is as follows:
C( ): base( ) { }

The colon and the accompanying base class constructor call are together known
as the constructor initializer.
Constructor Declarations
If the derived class does not explicitly call a base class constructor, the C#
compiler will implicitly use a constructor initializer of the form
:base( ).
This implies that a constructor declaration of the form
C( ) { }

is equivalent to
C( ): base( ) { }

Often this implicit behavior is perfectly adequate because:
 A class with no explicit base classes implicitly extends the System.Object
class, which contains a public parameterless constructor.
 If a class does not contain a constructor, the compiler will automatically
provide a public parameterless constructor called the default constructor.

Topic Objective
To describe how to invoke
the constructors of the base
class.
Lead-in
C# provides a keyword to
call a base class
constructor.
8 Module 10: Inheritance in C#


If a class provides an explicit constructor of its own, the compiler will not
create a default constructor. However, if the specified constructor does not
match any constructor in the base class, the compiler will generate an error as
shown in the following code:
class Token
{
protected Token(string name) { }
}

class CommentToken: Token
{
public CommentToken(string name) { } // Error here
}

The error occurs because the CommentToken constructor implicitly contains a
:base( ) constructor initializer, but the base class Token does not contain a
parameterless constructor. You can fix this error by using the code shown on
the slide.
Constructor Access Rules
The access rules for a derived constructor to call a base class constructor are
exactly the same as those for regular methods. For example, if the base class
constructor is private, then the derived class cannot access it:
class NonDerivable
{
private NonDerivable( ) { }
}

class Impossible: NonDerivable
{
public Impossible( ) { } // Compile-time error
}

In this case, there is no way for a derived class to call the base class constructor.
Module 10: Inheritance in C# 9


Scoping an Identifier
You can use the keyword base to also qualify the scope of an identifier. This
can be useful, since a derived class is permitted to declare members that have
the same names as base class members. The following code provides an
example:
class Token
{
protected string name;
}
class CommentToken: Token
{
public void Method(string name)
{
base.name = name;
}
}


Unlike in C++, the name of the base class, such as Token in the example
in the slide, is not used. The keyword base unambiguously refers to the base
class because in C# a class can extend one base class at most.

Note
10 Module 10: Inheritance in C#




 Implementing Methods
 Defining Virtual Methods
 Working with Virtual Methods
 Overriding Methods
 Working with Override Methods
 Using new to Hide Methods
 Working with the new Keyword
 Practice: Implementing Methods
 Quiz: Spot the Bugs

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
You can redefine the methods of a base class in a derived class when the
methods of the base class have been designed for overriding.
After completing this lesson, you will be able to:
 Use the virtual method type.
 Use the override method type.
 Use the hide method type.

Topic Objective
To provide an overview of
the topics covered in this
section.
Lead-in
In this lesson, you will learn
about implementing
methods in derived classes.

Không có nhận xét nào:

Đăng nhận xét