Sie sind auf Seite 1von 3

Advanced Visual Basic - Inheritance

Summary Inheritance is the ability to define classes that serve as the basis for derived classes. Derived classes inherit, and can extend, the properties, methods and events of the base class. Derived classes can also override inherited methods with new implementations. All classes created in VB are by default inheritable. he Inherits statement is used to declare a new class, called a derived class, based on an existin! class, "nown as a base class. Derived classes inherit, and can extend, the properties, methods, events, fields, and constants defined in the base class. Visual Basic allows only sin!le inheritance in classes, meanin! derived classes may have only one base class he access type of a derived class must be e#ual to or more restrictive than its base class $e.x. a Public class cannot inherit a Friend or a Private class, and a Friend class cannot inherit a Private class%

&ere is a list of class-level statements and modifiers to support inheritance' Inherits - Specifies the base class. NotInheritable - (revents pro!rammers from usin! the class as a base class. MustInherit - Specifies that the class is intended for use as a base class only. Instances of these "inds of classes cannot be created directly) they can only be created as base class instances of a derived class.

By default, a derived class inherits properties and methods from its base class. If an inherited property or method has to behave differently in the derived class it can be overridden. hat is, you can define a new implementation of the method in the derived class. he followin! modifiers are used to control how properties and methods are overridden' Overridable * Allows a property or method in a class to be overridden in a derived class.

Overrides * +verrides an +verridable property or method defined in the base class. NotOverridable * (revents a property or method from bein! overridden in an inheritin! class. By default, (ublic methods are ,ot+verridable. MustOverride * -e#uires that a derived class override the property or method. .hen this "eyword is used, the method definition consists of /ust the Sub, 0unction, or (roperty statement. ,o other statements are allowed, and specifically there is no 1nd Sub or 1nd 0unction statement. 2ust+verride methods must be declared in 2ustInherit classes.

3ou can use the MyBase "eyword to call methods in a base class when you override methods in a derived class. 0or example, suppose you are desi!nin! a derived class that overrides a method inherited from the base class. he over-ridden method can call the method in the base class and modify the return value MyBase refers to the base class and its inherited members. 4annot be used in modules. 4annot be used to #ualify itself. 4annot be used to access base class members that are mar"ed Private, nor Friend if the base class is in a different assembly.

0inally, you can use the "eyword MyClass to call an Overridable method implemented in your class and ma"e sure that the method in this class is called instead of an overriden method in a derived class. -efers to the containin! class and its inherited members. 4annot be assi!ned to a variable 4annot be used in standard modules. 4an be used to #ualify a method that is defined in a base class and that has no implementation of the method provided in that class. his has the same meanin! as 5 MyBase.Method 5

Sample 4ode

Class Class1 Sub Method1() MsgBox("This is a method in the base class.") End Sub Overridable Sub Method () MsgBox("This is another method in the base class.") End Sub End Class

Class Class !nherits Class1 "ublic #ield $s !nteger Overrides Sub Method () MsgBox("This is a method in a derived class.") End Sub End Class

"rotected Sub Test!nheritance() %im C1 $s &e' Class1 %im C $s &e' Class C1.Method1() ( Calls a method in the base class. C1.Method () ( Calls another method )rom the base class. C .Method1() ( Calls an inherited method )rom the base class. C .Method () ( Calls a method )rom the derived class. End Sub

Das könnte Ihnen auch gefallen