CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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
CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
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.
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.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
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.
Classes, Interfaces and Packages
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
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.
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.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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 Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Inheritance Basics Programming with Inheritance
Java Programming Language
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Computer Programming with JAVA
Fundaments of Game Design
CIS 199 Final Review.
Final and Abstract Classes
C++ Object Oriented 1.
Computer Science II for Majors
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CIS162AD Inheritance Part 3 09_inheritance.ppt

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

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

CIS162AD14 Abstract Methods  A virtual method in the base class has some actual code that can be executed.  An abstract method in the base class does NOT have any code – empty method.  Classes with abstract methods are designed to inherited.  Abstract methods must be overridden and implemented in the derived class.  Classes with abstract methods can not be used to instantiate an object.

CIS162AD15 Interface Classes  A interface class is similar to an abstract class.  Use keyword interface to declare class: interface IEmployee { … }  All methods in an interface are abstract methods.  When an interface is inherited, it is referred to implementing because all members must be implemented in the derived class.  Multiple interfaces can be inherited: public class HourlyEmp : IEmployee, IUser { … }

Delegates and Events  Coding delegates and events is an advanced topic, so the terms are just introduce here.  A delegate can be used to specify the signature of a method that can handle an event.  Signature is the number of and the data types of the parameters; not the name or return type.  An event is a signal that an action has occurred on an object.  An event declaration specifies a delegate that will handle the event.  Raise the event and pass the arguments required by the delegate. CIS162AD16

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

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

CIS162AD19 Using Inherited Classes private clsOrder cobjOrder; private clsOrderPreferred cobjOrderPreferred; if (preferredCheckBox.Checked) { cobjOrderPreferred = new clsOrderPreferred; cobjOrderPreferred.calcExtendedPrice( ); cobjOrderPreferred.accumulateTotals( ); } else { cobjOrder = new clsOrder; cobjOrder.calcExtendedPrice( ); cobjOrder.accumulateTotals( ); }

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

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