programming pillars introduction to object- oriented programming

23
Programming Pillars Introduction to Object- Oriented Programming

Upload: melvin-scott

Post on 31-Dec-2015

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Programming Pillars Introduction to Object- Oriented Programming

Programming Pillars

Introduction to Object-Oriented Programming

Page 2: 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

Page 3: Programming Pillars Introduction to Object- Oriented Programming

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

Page 4: Programming Pillars Introduction to Object- Oriented Programming

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

Page 5: Programming Pillars Introduction to Object- Oriented Programming

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

Page 6: Programming Pillars Introduction to Object- Oriented Programming

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

Page 7: Programming Pillars Introduction to Object- Oriented Programming

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

Page 8: Programming Pillars Introduction to Object- Oriented Programming

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

Page 9: Programming Pillars Introduction to Object- Oriented Programming

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

Page 10: Programming Pillars Introduction to Object- Oriented Programming

Slide 10

Inheritance and .NET Everything in .NET inherits from the

superclass named System.Object In Java, the superclass is named Object

Page 11: Programming Pillars Introduction to Object- Oriented Programming

Slide 11

Inheritance and .NET (Illustration)

Page 12: Programming Pillars Introduction to Object- Oriented Programming

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

{ }

Page 13: Programming Pillars Introduction to Object- Oriented Programming

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

Page 14: Programming Pillars Introduction to Object- Oriented Programming

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

Page 15: Programming Pillars Introduction to Object- Oriented Programming

Slide 15

Overriding Members (Example 1)

public class Person

{

public string FirstName; public string LastName;

public override string ToString() {

return FirstName + "," + LastName

}

}

Page 16: Programming Pillars Introduction to Object- Oriented Programming

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

Page 17: Programming Pillars Introduction to Object- Oriented Programming

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

Page 18: Programming Pillars Introduction to Object- Oriented Programming

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

Page 19: Programming Pillars Introduction to Object- Oriented Programming

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

Page 20: Programming Pillars Introduction to Object- Oriented Programming

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

Page 21: Programming Pillars Introduction to Object- Oriented Programming

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)

Page 22: Programming Pillars Introduction to Object- Oriented Programming

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

Page 23: Programming Pillars Introduction to Object- Oriented Programming

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