1 / 40

Chapter 11: Implementing Inheritance and Association

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.

chogan
Download Presentation

Chapter 11: Implementing Inheritance and Association

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11: Implementing Inheritance and Association Visual Basic .NET Programming: From Problem Analysis to Program Design

  2. 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

  3. 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

  4. Visual Basic .NET Programming: From Problem Analysis to Program Design

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Visual Basic .NET Programming: From Problem Analysis to Program Design

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Example 11-10: IPersonInterface Public Interface IPersonInterface Function TellAboutSelf() As String End Interface Visual Basic .NET Programming: From Problem Analysis to Program Design

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Visual Basic .NET Programming: From Problem Analysis to Program Design

  29. 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

  30. 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

  31. Visual Basic .NET Programming: From Problem Analysis to Program Design

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. Visual Basic .NET Programming: From Problem Analysis to Program Design

  38. 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

  39. 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

  40. 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

More Related