Download Inheritance - C Sharp Programming - Lecture Slides and more Slides C programming in PDF only on Docsity!
Inheritance
09_inheritance.ppt
Overview of Topics
- Inheritance
- Virtual Methods used for Overriding
- Constructors & Inheritance
- Abstract Classes and Methods
- Interface Classes
- Delegates and Events
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.
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.
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.).
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); }
}
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.
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; }
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.
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. } }
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.
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.
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.