1 / 20

Inheritance

Inheritance. Ability to define a new class from an existing class Purpose of Inheritance is reusability For example, each form created is inherited from the existing Form class Original class is called Base Class , Superclass, or Parent Class

Download Presentation

Inheritance

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. Inheritance • Ability to define a new class from an existing class • Purpose of Inheritance is reusability • For example, each form created is inherited from the existing Form class • Original class is called Base Class, Superclass, or Parent Class • Inherited class is called Subclass, Derived Class, or Child Class

  2. Inheritance (cont.) • Examine 1st line of code for a form in the Editor Inherited Class, Derived Class, Subclass, Child Class Public Class Form1 Inherits System.Windows.Forms.Form Base Class, Superclass, Parent Class

  3. Aggregation-”Has a” • Sometimes a class refers to other objects that are essential components of the class and don’t have an independent existence Public Class Car ‘Properties, pieces of the car Dim mtheEngine As Engine Dim mTransmissionAsTransmission ‘more code… End Class Still a “has-a” relationship. A Transmission is not a Car, it is just part of a Car

  4. Abstraction – “is-a” • Classes can be related in hierarchical ways • Animals: cats, dogs, birds • Vehicles: road vehicles – car, truck, bus; aircraft – helicopter, jet, blimp • Relationship is not the same as composition (“has-a”) • A cat “is-a” animal • Often, classes related this way have some behavior or state in common

  5. Person -Name -Address -Phone Employee Customer Student Inheritance Example • Base Class • Person • Derived Classes • Employee • Customer • Student

  6. Inheritance Implemented • New class can • Be based on another class (base class) • Inherit the properties and methods (but not constructors) of the base class, which can be • One of the VB existing classes • Your own class • Designate Inheritance by adding the Inherits statement referencing the base class

  7. Polymorphism: Overriding and Overloading • Different classes of objects may have behaviors that are named the same but are implemented differently (Overriding) • An object of a certain class might have several behaviors associated that are named the same but have different signatures (different type-number of arguments) and different implementation.(Overloading)

  8. Polymorphism Implemented • Overloading • Argument type-number (signature) determines which version of a method is used • Example: MessageBox.Show method • Overriding • Refers to a class that has the same method name as its base class • Method in subclass takes precedence

  9. Overloading Implemented • Overloading means that two or more methods have the same name but a different list of arguments. • Create it by giving the same name to multiple procedures in your class module, each with a different argument list and a different implementation.

  10. Overriding Methods • Methods created in subclass with the same name and the same argument list as the methods in the base class. • Subclass will use the method in its own class rather than that in the base class • To override a method • Declare the base class method with the Overridable keyword • Declare the subclass method with the Overrides keyword

  11. Animal -Name -Age -Noise Dog Cat Cow Example : Representing Animals • Base Class • Animal • Derived Classes • Dog • Cat • Cow

  12. Example : Representing Animals • Generic Animal Public Class Animal ‘properties, module level variables …. ‘Return the noise this animal makes Public Overridable Function noise( ) As String Return "?“ End Function End Class

  13. Cats Public Class Cat Inherits Animal ‘properties, module level variables …. ‘Return the noise cats makes Public Overrides Function noise( ) As String Return “Meow“ End Function End Class Dogs Public Class Dog Inherits Animal ‘properties, module level variables …. ‘Return the noise a dog makes Public Overrides Function noise( ) As String Return “Woof“ End Function End Class Specific Animals

  14. MyBase Keyword MyBaseis commonly used to access base methods that are overridden in a derived class. Public Class Animal Dim mstrMessage As String ‘Property Property message() As String ‘code …. End Property Public Overridable Function noise() As String Me.mstrMessage= "the noise is " End Function End Class Public Class Dog Inherits Animal Public Overrides Function noise() As String MyBase.noise() ‘MyBase takes all existing code in the ‘base class Return (Me.message & "Woof") End Function End Class

  15. Creating a Base Class Strictly for Inheritance • Classes can be created strictly for inheritance and are never instantiated • Subclasses are created and instantiated which inherit the base class properties and methods • For such a base class include the MustInherit modifier on the class declaration

  16. Object Browser • Use it to view the names, properties, methods, events and constants of VB objects, your own objects, and objects available from other applications • How to access it • Tab in Editor Window • View menu, Other Window, Object Browser

  17. Opening Object Browser from Toolbar

  18. Find Symbol Browse list Objects list Member list Namespace icons Method icon Property icon Constants icon Event icon Class icon Object Browser

  19. Examining VB Classes Members of System.Windows.Forms.MessageBox Class

  20. Examining VB Classes (cont.) Display the MessageBoxButtons Constants

More Related