Object-Oriented Programming: Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Object-Oriented Programming: Classes and Objects.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
These materials where developed by Martin Schray
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Microsoft Visual Basic 2005: Reloaded Second Edition
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Inheritance
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Inheritance
Chapter 6 Multiform Projects
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Java – Inheritance.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
CIS 199 Final Review.
Object-Oriented Programming: Inheritance
Presentation transcript:

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. Partial Public Class Form1 Inherits System.Windows.Forms.Form Inherited Class: Subclass, Derived Class, Child Class Original Class: Base Class, Superclass, Parent Class

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.

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

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

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.

Abstract Classes and Methods Declaring Abstract Classes and Abstract Methods ◦ You make a class abstract by declaring it with keyword MustInherit. ◦ An abstract class normally contains one or more abstract methods. ◦ An abstract method is declared with keyword MustOverride, as in Public MustOverride Sub Draw() ' abstract method

Abstract Classes and Methods MustOverride methods do not provide implementations. A class that contains any MustOverride methods must be declared as a MustInherit class even if it contains some concrete methods. Each concrete derived class of a MustInherit base class must provide concrete implementations of all the base class’s MustOverride methods. Constructors and Shared methods cannot be overridden, so they cannot be declared MustOverride.

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 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 ()

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 ()

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