programming pillars

10
Programming Pillars Inheritance Part 2

Upload: lilka

Post on 07-Jan-2016

27 views

Category:

Documents


2 download

DESCRIPTION

Programming Pillars. Inheritance Part 2. Lecture Overview. Abstract classes. Abstract Classes (Introduction). Abstract classes define an interface The implementation is either undefined or partially defined It's not possible to create an instance of an abstract class - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming Pillars

Programming Pillars

Inheritance Part 2

Page 2: Programming Pillars

Slide 2

Lecture Overview Abstract classes

Page 3: Programming Pillars

Slide 3

Abstract Classes (Introduction) Abstract classes define an interface

The implementation is either undefined or partially defined

It's not possible to create an instance of an abstract class

It's only possible to inherit from an abstract class The purpose of abstract classes

Use to create generalized classes Use to force standardized implementations of

derived classes

Page 4: Programming Pillars

Slide 4

Abstract Classes (Creating) In the abstract class, define the

interface for the class Declare an abstract class or member with

the abstract keyword in the class declaration

Page 5: Programming Pillars

Slide 5

Abstract Classes (Using) Define the implementation in the

derived class A derived class can inherit from an

abstract classA derived class must implement all abstract members

(those declared as abstract) A derived class can inherit members

implemented in the abstract class

Page 6: Programming Pillars

Slide 6

Abstract Classes (Example 1) Cryptography uses standard algorithms

to encrypt and decrypt messages There are different cryptographic

algorithms DES, RC2, Rijndael, TripleDES They all “work” the same way

They are all symmetric algorithms The SymmetricAlgorithm class is the

abstract class for all symmetric cryptographic algorithms

Page 7: Programming Pillars

Slide 7

Abstract Classes (Example 1)

Page 8: Programming Pillars

Slide 8

Abstract Classes (Example 2) Declare an abstract class named person

public abstract class Person

{}

Declare an abstract method namedpublic abstract bool

SetID(int argID);

Note that the method has NO implementation

Page 9: Programming Pillars

Slide 9

Implementing an Abstract Method (Example) Use the override keyword to declare

the implementation of an abstract method

public override bool SetID(

int argID)

{

RNumberHidden = argID;

}

Page 10: Programming Pillars

Slide 10

Prohibiting Inheritance A sealed class is a class that cannot be

inherited Declare sealed classes with the sealed

keyword The string class is not inheritable