Upload
mariah-lang
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
CIS162AD
Inheritance Part 3
09_inheritance.ppt
CIS162AD 2
Overview of Topics
Inheritance Virtual Methods used for Overriding Abstract Classes and Methods Interface Classes Delegates and Events Constructors & Inheritance
CIS162AD 3
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.
CIS162AD 4
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.
CIS162AD 5
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.
CIS162AD 6
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.
CIS162AD 7
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.).
CIS162AD 8
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); }}
CIS162AD 9
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.
CIS162AD 10
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.
CIS162AD 11
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;}
CIS162AD 12
Overriding calcExtendedPrice
In the derived class, clsOrderPreferred we override the method as follows:
public override void calcExtendedPrice ( ){
cdecExtendedPrice = cintQuantity * (cdecPrice - cdecPrice * cdecDISCOUNT_RATE);
}
CIS162AD 13
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.
CIS162AD 14
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.
CIS162AD 15
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.
CIS162AD 16
CIS162AD 17
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.
CIS162AD 18
Call Base Constructorspublic 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.
}}
CIS162AD 19
Using Inherited Classesprivate clsOrder cobjOrder;private clsOrderPreferred cobjOrderPreferred;
if (preferredCheckBox.Checked){
cobjOrderPreferred = new clsOrderPreferred;cobjOrderPreferred.calcExtendedPrice( );cobjOrderPreferred.accumulateTotals( );
}else{
cobjOrder = new clsOrder;cobjOrder.calcExtendedPrice( );cobjOrder.accumulateTotals( );
}
CIS162AD 20
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.
CIS162AD 21
Summary
Inheritance Virtual Methods used for Overriding Abstract Classes and Methods Interface Classes Delegates and Events Constructors & Inheritance