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

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Advanced Object Oriented Concepts Tonga Institute of Higher Education.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
OOP / VB.NET (Chp4) DR. JOHN ABRAHAM UTPA Credit: Partial slides from startvbdotnet.com.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 10: Introduction to Inheritance
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Object-Oriented Application Development Using VB.NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm – – –Web Site: websupport1.citytech.cuny.edu.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Object-Oriented Programming: Inheritance
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
Inheritance using Java
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Module 7: Object-Oriented Programming in Visual Basic .NET
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 8: Writing Graphical User Interfaces
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
Coming up: Inheritance
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
These materials where developed by Martin Schray
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
Microsoft Visual Basic 2005: Reloaded Second Edition
Object-Oriented Programming: Inheritance
One class is an extension of another.
Object-Oriented Programming: Inheritance
Abstract Classes.
Chapter 11 – Object-Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 14 Abstract Classes and Interfaces
Object-Oriented Programming: Inheritance
Presentation transcript:

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

Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Explore inheritance Create subclasses Write abstract classes and methods Define interfaces Create association links

Visual Basic.NET Programming: From Problem Analysis to Program Design3 Exploring Inheritance Base class –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 Design4

5 Writing a Superclass Definition Overridable keyword –Tells VB.NET that overriding method is permitted Override superclass method –Write method with same signature in subclass

Visual Basic.NET Programming: From Problem Analysis to Program Design6 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 Design7 Creating Subclasses Similar to writing any class definition Subclass must perform three tasks: –Specify superclass using keyword Inherits –Invoke superclass constructor as first statement in its constructor –Add keyword Overrides to methods overriding superclass methods MyBase keyword –Refers to superclass

Visual Basic.NET Programming: From Problem Analysis to Program Design8 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 Design9 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 Design10 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 Design11 Understanding Private versus Protected Access Private accessibility –No other object may access attributes directly –Includes subclasses Protected accessibility –Provides access to subclasses

Visual Basic.NET Programming: From Problem Analysis to Program Design12 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 Design13 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 Design14

Visual Basic.NET Programming: From Problem Analysis to Program Design15 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 Design16 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 Design17 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 Design18 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 Design19 Defining Interfaces (continued) Define interface containing abstract methods Require that classes implement methods in interface

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

Visual Basic.NET Programming: From Problem Analysis to Program Design21 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 Design22 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 Design23 Example 11-11: Student and Professor Implementing IPersonInterface (excerpt) 1. Public Class Student 2. Inherits Person 3. Implements IPersonInterface

Visual Basic.NET Programming: From Problem Analysis to Program Design24 Example 11-11: Student and Professor Implementing IPersonInterface (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 Design25 Example 11-11: Student and Professor Implementing IPersonInterface (continued) 1. Public Class Professor 2. Inherits Person 3. Implements IPersonInterface

Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 11-11: Student and Professor Implementing IPersonInterface (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 Design27 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 Design28

Visual Basic.NET Programming: From Problem Analysis to Program Design29 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 Design30 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 Design31

Visual Basic.NET Programming: From Problem Analysis to Program Design32 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 Design33 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 Design34 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 Design35 Example 11-15: Creating Student and Professor with 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 Design36 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 Design37

Visual Basic.NET Programming: From Problem Analysis to Program Design38 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 Design39 Summary (continued) Use keyword Overridable to 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 Design40 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