1 / 24

Object-Oriented Programming: Inheritance and Polymorphism

Learn about inheritance, a form of software reuse, and how it allows you to quickly create new classes by absorbing and customizing existing class members. Explore the concepts of base classes, derived classes, and the is-a relationship. Discover how polymorphism enables methods with the same name to have different implementations. Understand the inheritance of properties and methods, constructors in inheritance, and accessing properties in derived classes.

jolicoeur
Télécharger la présentation

Object-Oriented Programming: Inheritance and Polymorphism

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. Object-Oriented Programming: Inheritance and Polymorphism

  2. Introduction • Inheritance-, a form of software reuse in which a new class is created quickly and easily by absorbing an existing class’s members and customizing them with new or modified capabilities. • When creating a class, rather than declaring completely new members, you can designate that the new class inherits the members of an existing class. • The existing class is called the base class, and the new class is the derived class. • A derived class can add its own instance variables, Shared variables, properties and methods, and it can customize methods and properties it inherits.

  3. Base Classes and Derived Classes • Inheritance enables an is-a relationship. • In an is-a relationship, an object of a derived class also can be treated as an object of its base class.

  4. Base Classes and Derived Classes • A direct base class is the class from which a derived class explicitly inherits. • An indirect base class is inherited from two or more levels up in the class hierarchy.

  5. Inheritance • Ability to create a new class from an existing class • Purpose of inheritance is reusability. • For example, each form created is inherited from the existing Form class. • 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 • Use the Inherits statement following the class header and prior to any comments

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

  7. Polymorphism • Methods having identical names, but different implementations • Radio button, check boxes, and list boxes all have a Select method—the Select method operates appropriately for its class. • Overloading — Several argument lists for calling the method • Example: MessageBox.Show method • Overriding — Refers to a method that has the same name as its base class • Method in subclass takes precedence. • A base-class method must be declared Overridable if a derived class should be allowed to override the method with a version more appropriate for that class.

  8. Inheriting Properties and Methods • Public and protected data members and methods of the base class are inherited in the derived class. • Must write the method in the derived class if wanting to override the base-class method. • Can declare elements with the Protected keyword which specifies that the element is accessible only within its own class or any class derived from that class.

  9. Constructors in Inheritance • There is one exception for a derived class inheriting all public and protected methods: • A subclass cannot inherit constructors from the base class. • Each class must have its own constructors . • Exception is if the only constructor needed is an empty constructor — VB automatically creates an empty constructor for all classes.

  10. Constructors in Inheritance • Creating a derived-class object begins a chain of constructor calls in which the derived-class constructor, before performing its own tasks, invokes its direct base class’s constructor either explicitly (via the MyBase reference) • The original derived-class constructor’s body finishes executing last. • Each base class’s constructor initializes the base-class instance variables that are part of the derived-class object.

  11. Accessing Properties • Derived class can set and retrieve base class properties by using the property accessor methods. • Call the base class constructor from the derived class constructor to use the property values from the base class. • If the constructor requires arguments, the values can be passed when the constructor is called.

  12. Protected Members • A base class’s Protected members can be accessed only by members of that base class and by members of its derived classes. • In inheritance, Public members of the base class become Public members of the derived class, and Protected members of the base class become Protected members of the derived class. • A base class’s Private members are inherited but not directly accessible to its derived classes.

  13. Protected Members • Derived-class methods can refer to Public and Protected members inherited from the base class simply by using the member names. • Derived-class methods cannot directly access Private members of their base class. • A derived class can change the state of Private base-class instance variables only through Public and Protected methods provided in the base class and inherited by the derived class. • In most cases, it’s better to use Private instance variables to encourage proper software engineering.

  14. Protected Members • Using Protected instance variables creates several potential problems • Problems with Protected instance variables. • The derived-class object can set an inherited variable’s value directly without using a Set accessor. • Therefore, a derived-class object can assign an invalid value to the variable. • Derived-class methods are more likely to be written so that they depend on the base class’s data implementation.

  15. Overriding Methods • Methods with the same name and the same argument list as the base class • Derived class (subclass) will use the new method rather than the method in the base class. • To override a method • Declare the original method with the Overridable keyword. • Declare the new method with the Overrides keyword. • The access modifier for the base-class procedure can be Private or Protected (not Public).

  16. Abstract Classes and Methods • When we think of a class type, we assume that programs will create objects of that type. • In some cases, however, it’s useful to declare classes for which you never intend to instantiate objects. • Such classes are called abstract classes. • Because they’re typically used as base classes in inheritance hierarchies, we refer to them as abstract base classes. • These classes cannot be used to instantiate objects, because, as you’ll soon see, abstract classes are incomplete.

  17. Abstract Classes and Methods • Abstract base classes are too general to create real objects—they specify only what’s common among derived classes. • We need to be more specific before we can create objects. • For example, if you send the Draw message to abstract class TwoDimensionalShape, it knows that two-dimensional shapes should be drawable, but it does not know what specific shape to draw, so it cannot implement a real Draw method. • Concrete classes provide the specifics that make it reasonable to instantiate objects.

  18. Abstract Classes and Methods • The purpose of an abstract class is primarily to provide an appropriate base class from which other classes can inherit and thus share a common design. • In the Shape hierarchy below, derived classes inherit the notion of what it means to be a Shape—possibly including common properties such as Location, Color and Border-Thickness, and behaviors such as Draw, Move, Resize and ChangeColor.

  19. Using Multiple Forms • Projects can appear more professional when using different windows for different types of information. • Often, the first form a project displays is a summary form. • Projects can contain as many forms as desired.

  20. Creating New Forms • Select Add Windows Form from the Project menu and select from many installed templates.

  21. Modal versus Modeless Forms • Show method displays a form as modeless — means that both forms are open and the user can navigate from one form to the other • ShowDialog method displays a new form as modal — the user must respond to the form in some way, usually by clicking a button. • No other program code can execute until the user responds to and hides or closes the modal form. • With a modeless form, the user may switch to another form in the project without responding to the form.

  22. Show Method • General Form • Example • The Show method creates a form object from the specified class and displays it modelessly. The FormName is the name of the form to be displayed. FormName.Show () SummaryForm.Show ()

  23. ShowDialog Method • General Form • Example • Use the ShowDialog method when you want the user to notice, respond to, and close the form before proceeding with the application. FormName.ShowDialog () SummaryForm.ShowDialog ()

  24. Hiding or Closing a Form • The Close method behaves differently for a modeless form compared to a modal form. • Modeless — Close destroys the form instance and removes it from memory. • Modal — the form is only hidden. • Choosing a form’s Hide method sets the form’s Visible property to False and keeps the form instance in memory. • An example would be for a form with instructions or Help text

More Related