Programming Pillars
Introduction to Object-Oriented Programming
Slide 2
Review UML Class diagrams depict
the classes that make up a component the members of those classes The associations between classes
Class blocks are used to implement classes
The Visual Studio Class designer does some of the grunt work
Slide 3
Inheritance (Introduction) It’s all about generalization Inheritance involves an “is a”
relationship Examples
Graduate students and undergraduate students are both students
A graduate student is a student Faculty and classified staff are employees Students and employees are people
Slide 4
Inheritance (Goals) Delegate responsibilities from several
specific classes to a more generalized class More specific classes can then reuse the
code of more general classes Don’t create too many levels of
generalized classes There is no hard-and-fast rule though
Slide 5
Inheritance (UML) UML depicts a generalization using a
solid line and an unfilled arrow The arrow points from the specific class
to the general class
Slide 6
Inheritance (UML) (Illustration)
+New()+New(in argSeed : Integer)+Next() : Integer+Next(in maxValue : Integer) : Integer+Next(in minValue : Integer, in maxValue : Integer) : Integer+NextDouble() : Double
-CurrentRandom-HiddenNegativeRandom
NegativeRandom
Random
Slide 7
Inheritance (Terms 1) Inheritance is the ability of one class
to obtain, or inherit, all of the members of another class Base class - inherited class Derived class - inheriting class
Inheritance can be hierarchical One class can inherit from a base class,
which inherits from another class
Slide 8
Inheritance (Terms 2) Inheritance applies to all members
Sub procedures, Function procedures, Property procedures
Delegation: A derived class can leave the behavior alone
Extension: A derived class can add new behaviors to the base class
Overriding: A derived class can change the behavior of a member declared in the base class
Slide 9
Inheritance (Terms 3) By definition, all .NET Framework classes
support single inheritance A derived class can inherit from one and
only one base class Every class ultimately inherits from System.Object
Some languages support multiple inheritance
C++, for example Interfaces give us the means to get around the
single inheritance rule but we will not discuss interfaces in this class
Slide 10
Inheritance and .NET Everything in .NET inherits from the
superclass named System.Object In Java, the superclass is named Object
Slide 11
Inheritance and .NET (Illustration)
Slide 12
Inheriting a Class In VB, you would use the Inherits keyword In C#, the syntax is more terse
In the class declaration, the base class follows the derived class named separated by a “:”
Example: The Student class derives from the Person class
class Student : Person
{ }
Slide 13
Overriding Members Use overriding when a member in a
derived class should perform a different action than the member of the same name in a base class Use the override keyword in the derived
class member declaration It is not necessary to override all base
class members – just the ones whose actions should be changed
Slide 14
Overriding Members (Keywords) Optional virtual keyword appears in a
member declaration It indicates that the member may be
overridden in a derived class
Slide 15
Overriding Members (Example 1)
public class Person
{
public string FirstName; public string LastName;
public override string ToString() {
return FirstName + "," + LastName
}
}
Slide 16
Preventing and Forcing Inheritance abstract keyword appears in the declaration
for a member in a base class Use to declare an abstract member Derived class must provide an implementation for
the abstract member sealed keyword indicates that a class cannot
be inherited Use to create a sealed class
Slide 17
base (Introduction) The base keyword is used to call a
method in a base class Use base to augment a base class method
Rules: The call to base must be the first
statement in the method body It’s illegal to assign base
Slide 18
Using base base keyword works like an object
variable Use in a derived class to call a member in
a base class Use base to call a constructor in a base
class base is not a real object
Cannot be used in modules because modules cannot be inherited
Slide 19
Inheritanceand Constructors (Introduction)
Constructors have different inheritance rules than other procedures
Constructors without arguments are implicitly inherited
Constructors with arguments are not implicitly inherited
Slide 20
Default Constructors The constructor (without arguments) in
the base class is called from the constructor in the derived class Inheritance is “automatic” The statements in the base class
constructor are called before the statements in the current (derived) class execute
MyBase is optional but not required
Slide 21
Default Constructors (Call Sequence)
Public Class Base1 Public Sub New()
' Statements End SubEnd Class
Public Class DerivedFromBase1 Inherits Base1
Public Sub New()' Statements
End SubEnd Class
Public Class DerivedFromBase2 Inherits DerivedFromBase1
Public Sub New() MyBase.New()
' Statements End SubEnd Class
Call base class constructor (1)
Call base class constructor (2)
Return (4)
Return (3)
Slide 22
Custom Constructors Constructors with arguments are not implicitly
inherited Usage rules:
In the base class, call base base MUST be the first statement in the
constructor Call base with the desired argument to call
the appropriate base class constructor Remember that standard overloading rules
apply
Slide 23
Inheritance and Shared Members Shared members CANNOT be
overridden Shared members can be inherited "as is" Shared members can be shadowed and
redefined from scratch base is not allowed in shared members
Top Related