30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10 Object-Oriented <strong>Program</strong>ming: Polymorphism 439<br />

A class declared NotInheritable cannot be inherited from, and every method in it is implicitly<br />

NotOverridable.<br />

In <strong>Visual</strong> <strong>Basic</strong>, it is impossible <strong>to</strong> pass a method reference directly as an argument <strong>to</strong> another<br />

method. To address this problem, <strong>Visual</strong> <strong>Basic</strong> allows the creation of delegates, which are classes<br />

that encapsulate a set of references <strong>to</strong> methods.<br />

Keyword AddressOf returns a reference <strong>to</strong> a delegate method. <strong>Visual</strong> <strong>Basic</strong> implicitly converts<br />

the method reference in<strong>to</strong> a delegate object that contains the method reference.<br />

TERMINOLOGY<br />

abstract method multicast delegate<br />

abstract class MustInherit base class<br />

AddressOf keyword MustOverride method<br />

cast NotInheritable class<br />

class declared NotInheritable NotOverridable method<br />

class hierarchy object-oriented programming (OOP)<br />

concrete class Overridable method<br />

delegate Overrides keyword<br />

information hiding polymorphic programming<br />

inheritance polymorphism<br />

inheritance hierarchy reference type<br />

interface references <strong>to</strong> abstract base class<br />

“is-a” relationship Select Case logic<br />

InvalidCastException<br />

method reference<br />

singlecast delegate<br />

SELF-REVIEW EXERCISES<br />

10.1 Fill in the blanks in each of the following statements:<br />

a) Treating a base-class object as a can cause errors.<br />

b) Polymorphism helps eliminate logic.<br />

c) If a class contains one or more MustOverride methods, it is an class.<br />

d) Classes from which objects can be instantiated are called classes.<br />

e) Classes declared with keyword cannot be inherited.<br />

f) An attempt <strong>to</strong> cast an object <strong>to</strong> one of its derived types can cause an .<br />

g) Polymorphism involves using a base-class reference <strong>to</strong> manipulate .<br />

h) Abstract classes are declared with the keyword.<br />

i) Class members can be overridden only with the keyword.<br />

j) are classes that encapsulate references <strong>to</strong> methods.<br />

10.2 State whether each of the following is true or false. If false, explain why.<br />

a) All methods in a MustInherit base class must be declared MustOverride.<br />

b) Referring <strong>to</strong> a derived-class object with a base-class reference is dangerous.<br />

c) A class with a MustOverride method must be declared MustInherit.<br />

d) Methods that are declared MustOverride still must be implemented when they are declared.<br />

e) Classes declared with the NotInheritable keyword cannot be base classes.<br />

f) Polymorphism allows programmers <strong>to</strong> manipulate derived classes with references <strong>to</strong> base<br />

classes.<br />

g) Polymorphic programming can eliminate the need for unnecessary Select-Case logic.<br />

h) Use keyword MustInherit <strong>to</strong> declare an abstract method.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!