Object-Oriented Programming: Inheritance and Polymorphism

Object-Oriented
Programming:
Inheritance and
Polymorphism
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.
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.
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.
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
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
Polymorphism

Methods having identical names, but different
implementations


Overloading — Several argument lists for calling the
method


Radio button, check boxes, and list boxes all have a Select
method—the Select method operates appropriately for its
class.
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.
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.
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.
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.
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.
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.
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 baseclass 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.
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.
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).
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.
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 twodimensional 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.
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.
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.
Creating New Forms

Select Add Windows Form from the Project menu and
select from many installed templates.
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.
Show Method

General Form
FormName.Show ()

Example
SummaryForm.Show ()

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.
ShowDialog Method

General Form
FormName.ShowDialog ()

Example
SummaryForm.ShowDialog ()

Use the ShowDialog method when you want the user to
notice, respond to, and close the form before
proceeding with the application.
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