400 likes | 541 Views
Chapter 11: Implementing Inheritance and Association. Visual Basic .NET Programming: From Problem Analysis to Program Design. Objectives. Explore inheritance Create subclasses Write abstract classes and methods Define interfaces Create association links. Exploring Inheritance.
E N D
Chapter 11: Implementing Inheritance and Association Visual Basic .NET Programming: From Problem Analysis to Program Design
Objectives • Explore inheritance • Create subclasses • Write abstract classes and methods • Define interfaces • Create association links Visual Basic .NET Programming: From Problem Analysis to Program Design
Exploring Inheritance • Baseclass • Serves as base for subclasses • Derived class • Also called subclass • Derive some attributes and behavior from superclass • When you instantiate subclass • Actually instantiate inheritance hierarchy Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Writing a Superclass Definition • Overridable keyword • Tells VB .NET that overriding method is permitted • Overridesuperclass method • Write method with same signature in subclass Visual Basic .NET Programming: From Problem Analysis to Program Design
Instantiating Superclasses • From Example 11-2: 5. Dim person1, person2, person3 As Person 6. ' create 3 instances of Person 7. person1 = New Person(“Eleanor”, “Atlanta”) 8. person2 = New Person(“Emily”, “St. Louis”) 9. person3 = New Person(“Graham”, “Marietta”) Visual Basic .NET Programming: From Problem Analysis to Program Design
Creating Subclasses • Similar to writing any class definition • Subclass must perform three tasks: • Specify superclass using keyword Inherits • Invoke superclass constructor as firststatement in its constructor • Add keyword Overridesto methods overriding superclass methods • MyBase keyword • Refers to superclass Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-3: Student Class Definition (excerpt) 1. Public Class Student 2. Inherits Person 6. Public Sub New(ByVal aName As String, ByVal anAddress As String, ByVal anId As Integer, ByVal aMajor As String) 7. ' invoke superclass constructor to populate name & address attributes 8. MyBase.New(aName, anAddress) Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-3: Student Class Definition (excerpt) 27. Public Overrides Function TellAboutSelf() As String 28. Dim info As String 29. info = MyBase.TellAboutSelf() & ", " & GetId() & ", " & GetMajor() 30. Return info 31. End Function Visual Basic .NET Programming: From Problem Analysis to Program Design
Instantiating an Inheritance Hierarchy • When instantiating subclass • Create composite instance containing attributes and methods of both superclass and subclass • Client does not know about superclass Visual Basic .NET Programming: From Problem Analysis to Program Design
Understanding Private versus Protected Access • Private accessibility • No other object may access attributes directly • Includes subclasses • Protectedaccessibility • Provides access to subclasses Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-5: Person with Protected Methods (excerpt) 5. Protected Sub New(ByVal aName As String, ByVal anAddress As String) 6. SetName(aName) 7. SetAddress(anAddress) 8. End Sub Visual Basic .NET Programming: From Problem Analysis to Program Design
Adding a Second Subclass • Can add more than one subclass to superclass • Superclass does not know subclasses exist Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Writing Abstract Classes and Methods • Abstract class • Class that cannot be instantiated • Includes keyword MustInherit in header • Concrete class • Class that can be instantiated Visual Basic .NET Programming: From Problem Analysis to Program Design
Writing Abstract Classes and Methods (continued) • Abstract method • Method that consists only of signature • Has no code • Require all subclasses to implement method • Method signature contains keyword MustOverride Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-9: Abstract Person Class with Abstract Method (excerpt) 1. Public MustInherit Class Person 23. Public MustOverride Function TellAboutSelf() As String Visual Basic .NET Programming: From Problem Analysis to Program Design
Defining Interfaces • Adding abstract method to class • Requires that subclasses override method • Assures and enforces standardization • Another way to require that methods be included in classes is to define an interface • Interface • VB .NET component • Defines abstract methods that must be implemented Visual Basic .NET Programming: From Problem Analysis to Program Design
Defining Interfaces (continued) • Define interface containing abstract methods • Require that classes implement methods in interface Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-10: IPersonInterface Public Interface IPersonInterface Function TellAboutSelf() As String End Interface Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-10: IPersonInterface (continued) • Interface • Begins with header Public Interface • Ends with the key words End Interface • Naming convention: • Name begins with capital letter “I” • All functionality must be defined by class that implements interface Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-10: IPersonInterface (continued) • VB .NET class • Can inherit from only one superclass • Can also implement one or more interfaces Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-11: Student and Professor ImplementingIPersonInterface (excerpt) 1. Public Class Student 2. Inherits Person 3. Implements IPersonInterface Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-11: Student and Professor ImplementingIPersonInterface (continued) 12. Public Function TellAboutSelf() As String Implements IPersonInterface.TellAboutSelf 13. Dim info As String 14. info = GetName() & ", " & GetAddress() & ", " & GetId() & ", " & GetMajor() 15. Return info 16. End Function Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-11: Student and Professor ImplementingIPersonInterface (continued) 1. Public Class Professor 2. Inherits Person 3. Implements IPersonInterface Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-11: Student and Professor ImplementingIPersonInterface (continued) 12. Public Function TellAboutSelf() As String Implements IPersonInterface.TellAboutSelf 13. Dim info As String 14. info = GetName() & ", " & GetAddress() & ", " & GetRank() & ", " & GetDepartment() 15. Return info 16. End Function Visual Basic .NET Programming: From Problem Analysis to Program Design
Creating Association Links • Two or more classes often have association relationships • Implement association relationships by creating links between instances of classes Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Implementing One-to-One Association • One-to-one association example • Each person has one phone • Each phone is owned by one person • One-to-one association • Each class has attribute referencing other class Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-12: Person Class Definition with Phone Association (excerpt) 1. Public MustInherit Class Person 2. Private name As String 3. Private address As String 4. Private phone As Phone Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Implementing One-to-Many Associations • Association relationships include • One-to-one • One-to-many • Many-to-many • Often implemented using: • Arrays • ArrayLists Visual Basic .NET Programming: From Problem Analysis to Program Design
Implementing One-to-Many Associations (continued) • Implement association relationships by • Connecting instances of classes having association relationships • Connections are accomplished by creating and populating attribute reference variables Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-14: Person Class Definition with Multiple Phones (excerpt) 1. Public MustInherit Class Person 2. Private name As String 3. Private address As String 4. Private phones As ArrayList Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 11-15: Creating Student and Professorwith Multiple Phones (excerpt) 3. Module Client 4. Sub Main() 5. Dim student1 As Student, professor1 As Professor 6. Dim phoneInfo() As String, i As Integer Visual Basic .NET Programming: From Problem Analysis to Program Design
Programming Example: Student Enrolls In Course • Demonstrates implementation of inheritance and many-to-many association involving subclass Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary • Inheritance • Superclass specifies general characteristics of object • Subclass inherits characteristics from superclass and adds more specialized attributes and methods • Writing definition for subclass is similar to writing any class Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary (continued) • Use keyword Overridableto tell VB .NET that overriding method is permitted • Private accessibility • Can only be accessed within class • Protected accessibility • Can be accessed within class and subclasses • Abstract class • Class that you do not instantiate Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary (continued) • Abstract method • Method that consists only of signature • Interface • Defines abstract methods that must be implemented • Association relationships: • One-to-one • One-to-many • Many-to-many Visual Basic .NET Programming: From Problem Analysis to Program Design