CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Juan Marquez 12_inheritance.ppt
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
More about classes and objects Classes in Visual Basic.NET.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advanced Object-Oriented Programming Features
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 13: Object-Oriented Programming
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
CIS162AD - C# Methods, Menus, and Dialog Boxes 05_methods_menus.ppt.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Applications Development
Programming in Java CSCI-2220 Object Oriented Programming.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Introduction to Object-Oriented Programming Lesson 2.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
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)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Microsoft Visual Basic 2005: Reloaded Second Edition
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
OOP’S Concepts in C#.Net
C# Event Processing Model
Inheritance Basics Programming with Inheritance
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Fundaments of Game Design
CIS 199 Final Review.
C++ Object Oriented 1.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CIS162AD Inheritance 09_inheritance.ppt

CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance  Abstract Classes and Methods  Interface Classes  Delegates and Events

CIS162AD3 Class and Object  A class is a definition of a new data type.  A class definition includes member variables and methods.  An object is a variable declared using a class as the data type.

CIS162AD4 Inheritance  Inheritance is the process by which a new class is created from an existing class, but the new class has additional member variables and/or methods.  This is a powerful feature of object-oriented languages.  This allows us to reuse code that has already been developed and tested.

CIS162AD5 Inheritance Terminology  Base Class  Parent Class  Super Class Derived Class Child Class Sub Class A derived class would be able to do everything the base class can do, plus it would add some attributes or operations to increase its functionality.

CIS162AD6 Inheritance Possibilities  When creating a new class, we can use –An existing class (form, button, etc.) –A programmer defined class (clsOrder, clsCustomer)  When designing an application, we should recognize classes that lend themselves to inheritance.  When there are two or more classes with similar properties and methods, we should create a base class that contains the common items to reduce duplicate code.

CIS162AD7 Data Inheritance  In CS8 we created a class to process orders, clsOrder.  In CS9 we want to create a new class, clsOrderPreferred, to handle orders for preferred customers, which would include a discount in the calculation.  If we went through the design phase, we would see that the class would include many of the items and calculations already defined in clsOrder.  The best thing to do would be to inherit clsOrder, and then add or redefine properties and methods that are needed for a preferred order (Discount Rate, calcExtendedPrice with discount, etc.).

CIS162AD8 clsOrderPreferred //Add only the variables and methods needed for a preferred Order public class clsOrderPreferred : clsOrder { const decimal cdecDISCOUNT_RATE = 0.05M; //5% public override void calcExtendedPrice( ) { cdecExtendedPrice = cintQuantity * (cdecPrice - cdecPrice * cdecDISCOUNT_RATE); }

CIS162AD9 Method Overloading - reviewed  Method Overloading occurs when there are two methods with the same name, but differ in the number or data types of the parameters. MessageBox.Show(TextMessage); MessageBox.Show(TextMessage, TitleBar); MessageBox.Show(TextMessage, TitleBar, ButtonType); MessageBox.Show(TextMessage, TitleBar, ButtonsType, Icon);  C# determines which method is executed based on the arguments provided in the method call.  Overloading and Overriding are forms of Polymorphism.  Polymorphism means the ability to take on many shapes or forms.

CIS162AD10 Method Overriding  Method Overriding occurs when a method in the derived class has the exact same name and parameters as a method in the base class – same signature.  C# determines which method is executed based on the class the object was created from. –If the object is created using the base class, then the base method is called. –If the object is created using the derived class, then the overriding method is called.  We override methods because we want a standard name for our methods across classes, such calcExtendedPrice instead of calcExtendedPrice1, calcExtendedPrice2, etc.

CIS162AD11 Virtual calcExtendedPrice is Overridable  In clsOrder there is a calcExtendedPrice.  If we wanted to create a clsOrderPreferred that would calculated the extended amount with a discount, we can redefine calcExtendedPrice in the new class by Overriding it.  To allow the calcExtendedPrice method to be overridden, declare method as virtual n the base class, clsOrder: public virtual void calcExtendedPrice ( ) { cdecExtendedPrice = cintQuantity * cdecPrice; }

CIS162AD12 Overriding calcExtendedPrice  In the derived class, clsOrderPreferred we override the method as follows: public override void calc ExtendedPrice ( ) { cdecExtendedPrice = cintQuantity * (cdecPrice - cdecPrice * cdecDISCOUNT_RATE); }

CIS162AD13 Constructors & Inheritance  Constructors are NOT inherited.  Each derived class must have a constructor defined, and it should call the constructor from the base class.  The base constructor would be responsible to initialize variables in the base class, so the derived class would only need its constructor to initialized variables defined in its class.  Do not duplicate code.  Pass the parameter values to the constructor in the base class so it can assign the parameter values to the class variables through the property methods.  Use the existing code in the base class.

CIS162AD14 Call Base Constructors public class clsOrderPreferred : clsOrder { //The descr, qty, price and all the Get and Put methods are inherited //Only add the new properties and/or methods public clsOrderPreferred( ) : base( ) { //Call default constructor in base class } public clsOrderPreferred (string descr, int qty, decimal price) : base (descr, qty, price) { //Call overloaded constructor in base class //to assign parameter values to class variables. //Do not duplicate the code to assign values. } }

CIS162AD15 Using Inherited Classes if (preferredCheckBox.Checked) { clsOrderPreferred cobjOrder = new clsOrderPreferred; cobjOrder.calcExtendedPrice( ); cobjOrder.accumulateTotals( ); } else { clsOrder cobjOrder = new clsOrder; cobjOrder.calcExtendedPrice( ); cobjOrder.accumulateTotals( ); } //each object calls a different calcExtendedPrice( )

CIS162AD16 Inheritance and Form class  In all of the programs we have written so far we have seen the following statement: public partial class CS8Form : Form  The form we create is based on a predefined Form class that includes such things as: –Properties:.Font,.AcceptButton,.CancelButton, –Methods:.Close( ),.Activate( ),.Hide( ).  We then add our own buttons, textboxes, etc.

CIS162AD17 Abstract Classes  An abstract class can NOT be instantiated.  It can only be used as a base class that other classes can inherit.  Use keyword abstract to declare class: public abstract class Employee { … }  Abstract classes may contain method implementations or abstract methods.

CIS162AD18 Abstract Methods  A virtual method in the base class has some actual code that can be executed, so overriding is optional.  An abstract method in the base class does NOT have any code – empty method. public abstract void printCheck( );  Abstract methods must be overridden and implemented in the derived class.

CIS162AD19 Interface Classes  A interface class is similar to an abstract class.  Use keyword interface to declare class and usually named with a capital I prefix: public interface IEmployee { decimal calcGross( ); //no implementation }  All methods in an interface are basically abstract methods – no implementation included.

Delegates and Events  Coding delegates and events is an advanced topic, so the terms are just introduce here.  A delegate is used to specify the signature of a method that can handle an event.  EventHandler is a predefined delegate in System: Public delegate void EventHandler(object sender, EventArgs e);  Next indicate the method that a delegate should call. New System.EventHandler(this.clickButton_Click); This code found in designer file.  The event sender controls the generation of events and calls the defined event handler. CIS162AD20

CIS162AD21 Summary  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance  Abstract Classes and Methods  Interface Classes  Delegates and Events