Chapter 8Thing To KnowOther Good StuffScopeCreating SubclassesUsing the Inherits Keyword to Create the Sailboat SubclassUsing the Inherits Keyword to Create a SubclassUnderstanding Abstract and Final ClassesUsing the NotInheritable KeywordOverriding a Superclass MethodOverriding MethodsOverriding and Invoking a Superclass MethodOverriding, Polymorphism, and Dynamic BindingIntroducing the Lease Subclasses and Abstract MethodsAdding an Abstract MethodsUnderstanding and Using InterfacesUnderstanding and Using InterfacesCreating a VB .NET InterfaceCreating a VB .NET InterfaceUsing Custom ExceptionsUnderstanding the Object Class and InheritanceUnderstanding the Object Class and InheritanceChapter 9Things to KnowAssociation RelationshipsIdentifying Association Relationships on Class DiagramIdentifying Association RelationshipsAssociating VB .NET Classes in a One-to-One RelationshipAdding Functionality to the Boat ClassSlide 30A One-to-Many Association RelationshipAssociating Docks and Slips: A One-to-Many Association RelationshipSlide 3311Chapter 8Chapter 8Understanding Understanding Inheritance and Inheritance and InterfacesInterfaces22Thing To KnowThing To KnowGeneralization/Specialization Generalization/Specialization class hierarchyclass hierarchySuper class and sub classSuper class and sub classAbstract classesAbstract classesMustInheritMustInheritFinal classes Final classes NotInheritableNotInheritableConcrete classesConcrete classesPolymorphismPolymorphismDynamic BindingDynamic BindingOverriding a superclass methodOverriding a superclass methodOveridableOveridableOverRidesOverRidesPrivate versus Protected accessPrivate versus Protected accessInterfacesInterfacesCreating custom exceptionsCreating custom exceptionsThe Object class and inheritanceThe Object class and inheritance33Other Good StuffOther Good StuffMethod SignaturesMethod SignaturesNameNameParameter ListParameter ListReturn TypeReturn TypeReference VariablesReference VariablesNothingNothingDateTimeDateTimeNowNowTodayTodayParameter ListsParameter ListsByValByValByRefByRefPrivatePrivateNo other object can directly No other object can directly read or modify the valuesread or modify the values•Must use methods of the Must use methods of the class to get or set valuesclass to get or set valuesEnsures encapsulation and Ensures encapsulation and information hidinginformation hidingSubClass must use SubClass must use superclass’ accessorsuperclass’ accessor44ScopeScopePrivatePrivateNo other object can directly No other object can directly read or modify the valuesread or modify the values•Must use methods of the Must use methods of the class to get or set valuesclass to get or set valuesEnsures encapsulation and Ensures encapsulation and information hidinginformation hidingSubClass must use SubClass must use superclass’ accessorsuperclass’ accessorProtectedProtectedValues can be directly Values can be directly accessed by subclasses accessed by subclasses Local variableLocal variableAccessible only to Accessible only to statements within a method statements within a method where it is declaredwhere it is declaredExists only as long as the Exists only as long as the method is executingmethod is executing55Creating SubclassesCreating Subclasses Generalization/specialization hierarchyGeneralization/specialization hierarchySuperclassSuperclass•Includes attributes and methods that are common to specialized subclassesIncludes attributes and methods that are common to specialized subclassesInstances of the subclassesInstances of the subclasses•Inherit attributes and methods of the superclassInherit attributes and methods of the superclass•Include additional attributes and methodsInclude additional attributes and methods Inherits keywordInherits keywordUsed in the class header to implement a subclassUsed in the class header to implement a subclassIndicates which class the new class is extendingIndicates which class the new class is extendingExample: Example: •Class header to define the Sailboat class as a subclass of Boat:Class header to define the Sailboat class as a subclass of Boat:Public Class SailboatPublic Class SailboatInherits BoatInherits Boat66Using the Using the Inherits Inherits Keyword to Keyword to Create the Sailboat SubclassCreate the Sailboat Subclass77Using the Using the Inherits Inherits Keyword to Keyword to Create a SubclassCreate a SubclassSub class constructor mustSub class constructor mustMyBase.New callMyBase.New call•Used to set attributes for the superclass Used to set attributes for the superclass •Must be the first statement in the constructorMust be the first statement in the constructor•Required unless the superclass includes a default Required unless the superclass includes a default constructor without parametersconstructor without parametersCalls to SubClass accessor methods for Calls to SubClass accessor methods for setting SubClass attributessetting SubClass attributes88Understanding Abstract and Final Understanding Abstract and Final ClassesClasses Concrete classesConcrete classesClasses that can be instantiatedClasses that can be instantiatedAbstract classAbstract classNot intended to be instantiatedNot intended to be instantiatedOnly used to extend into subclassesOnly used to extend into subclassesFacilitates reuseFacilitates reuseMustInherit keywordMustInherit keywordUsed in class header to declare an abstract class Used in class header to declare an abstract class Example: Example: •Class header to make the Boat class abstract: Class header to make the Boat class abstract: Public MustInherit Class BoatPublic MustInherit Class Boat99Using the NotInheritable KeywordUsing the NotInheritable Keyword A Final classA Final classA class that cannot be extendedA class that cannot be extendedCreated for security purposes or efficiencyCreated for security purposes or efficiencyCreated using the NotInheritable keywordCreated using the NotInheritable keywordExampleExample•Class header for the Payroll class :Class header for the Payroll class :Public NotInheritable Class Payroll Public NotInheritable Class Payroll Inherits EmployeeInherits Employee1010Overriding a Superclass MethodOverriding a Superclass Method Method overridingMethod overridingMethod in subclass will be
View Full Document