Derived classes can modify the behavior of base class methods, including those related to events.
☰Fullscreen
Table of Content:
In object-oriented programming, a derived class is a class that inherits from another class, referred to as the base class. Derived classes can extend or modify the behavior of the base class.
In the context of the OnNewIssue
method, the term "derived classes" refers to classes that inherit from the MagazineCompany
class and potentially override the OnNewIssue
method to change how the event is raised.
Here's an example to illustrate:
Step-by-Step
- Base Class (
MagazineCompany
): - Custom
EventArgs
Class: - Derived Class (
SpecialMagazineCompany
): - Event Handler:
- Example Usage:
public class MagazineCompany { public delegate void NewIssueEventHandler(object sender, NewIssueEventArgs e); public event NewIssueEventHandler NewIssue; public void PublishNewIssue(int issueNumber, string issueTitle) { Console.WriteLine("Magazine Company: Publishing new issue..."); OnNewIssue(new NewIssueEventArgs(issueNumber, issueTitle)); } // Protected virtual method protected virtual void OnNewIssue(NewIssueEventArgs e) { NewIssue?.Invoke(this, e); } }
public class NewIssueEventArgs : EventArgs { public int IssueNumber { get; } public string IssueTitle { get; } public NewIssueEventArgs(int issueNumber, string issueTitle) { IssueNumber = issueNumber; IssueTitle = issueTitle; } }
public class SpecialMagazineCompany : MagazineCompany { // Override the OnNewIssue method to add custom behavior protected override void OnNewIssue(NewIssueEventArgs e) { // Custom behavior: log the event before invoking it Console.WriteLine($"SpecialMagazineCompany: Logging issue {e.IssueNumber} titled '{e.IssueTitle}' before raising the event."); // Call the base class method to raise the event base.OnNewIssue(e); } }
public class Reader { public void OnNewIssueReceived(object sender, NewIssueEventArgs e) { Console.WriteLine($"Reader: Received new issue {e.IssueNumber} titled '{e.IssueTitle}'."); } }
public class Program { public static void Main(string[] args) { // Create a special magazine company (Publisher) SpecialMagazineCompany specialMagazineCompany = new SpecialMagazineCompany(); // Create a reader (Subscriber) Reader reader = new Reader(); // Subscribe the reader to the magazine company's new issue event specialMagazineCompany.NewIssue += reader.OnNewIssueReceived; // Publish a new issue with additional event data specialMagazineCompany.PublishNewIssue(42, "The Future of Technology"); } }
Explanation
- Base Class (
MagazineCompany
): This class defines the event and a method to raise the event (OnNewIssue
). The method is marked asprotected virtual
, allowing derived classes to override it. - Derived Class (
SpecialMagazineCompany
): This class inherits fromMagazineCompany
and overrides theOnNewIssue
method. In this override, it adds custom behavior (logging) before calling the base class'sOnNewIssue
method. - Event Handler (
Reader
): This class subscribes to the event and handles it by printing a message. - Example Usage (
Program
): This demonstrates creating an instance of the derived class, subscribing to the event, and publishing a new issue. The derived class's custom behavior (logging) will be executed before the event is raised.
When you run the program, the output will be:
Magazine Company: Publishing new issue... SpecialMagazineCompany: Logging issue 42 titled 'The Future of Technology' before raising the event. Reader: Received new issue 42 titled 'The Future of Technology'.
This shows how derived classes can modify the behavior of base class methods, including those related to events.