Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance."— Presentation transcript:

1 CIS162AD Inheritance 09_inheritance.ppt

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

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

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

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

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

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

8 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); }

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

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

11 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; }

12 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); }

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

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

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

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

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

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

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

20 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

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


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

Similar presentations


Ads by Google