Searching...

Translate

Events

*      2.6.3 Events

   An event is a delegate type class member that is used by the object or class to provide a notification to other objects that an event has occurred. We can also say that an event is, essentially, an automatic notification that some action has occurred.

   The client object can act on an event by adding an event handler to the event.

Ø 2.6.3.1 Generating events

   Events are declared using the simple event declaration format as follows:

modifier event type event-name;

   The modifier may be new, a valid combination of the four access modifiers, and a valid combination of static, virtual, override, abstract and sealed.

   The type of an event declaration must be a delegate type and the delegate must be as accessible as the event itself.

   The event-name is any valid C# variable name. event is a keyword that signifies that the event-name is an event.

   Examples of event declaration are:

public static event delsimple invite;

   Since events are based on delegates, we must first declare a delegate and then declare an instance of the delegate using the keyword event. Below program illustrates a simple implementation of an event handler.

using System;

    delegate void delsimple();
    class Program
    {
        public static event delsimple invite;
        public static void welcome()
        {
            Console.WriteLine("Welcome to event");
        }
        static void Main(string[] args)
        {
            invite += new delsimple(welcome);
            invite();
            Console.ReadLine();
        }

    }

   Output:

Welcome to event


*   2.7 Inheritance


   “The mechanism of designing or construction one class from another is called inheritance”.

   Inheritance is one of the three foundational principles of object-oriented programming because it allows the creation of hierarchical classifications.

   Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

   In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called a derived class.

   Inheritance provides reusability it consumes less time. The properties and functionality of a parent class is derived in child class.

*      2.7.1 Types of inheritance

§  Single inheritance(only one base class)
§  Multiple inheritance(several base classes)
§  Hierarchical inheritance(one base class, many subclasses)
§  Multilevel inheritance(derived from a derived class)

   C# does not directly implement multiple inheritance. However, this concept is implemented using a secondary inheritance path in the form of interface.




Ø Single inheritance(only one base class)


   Let us consider two classes A and B. we can create a class hierarchy such that B is derived from A as shown in fig.

   Class a, the initial class that is used as the basis for the derived class is referred to as the base class, parent class or super class.

   Class B, the derived class, is referred to as derived class, child class or sub class.
   A derived class is a completely new class that incorporates all the data and methods of its base class. It can also have its own data and method members that are unique to itself.

   When only one derived class is derived from base class than this type of inheritance is known as “single level inheritance”.

   Example of single level inheritance is illustrated as below.









using System;


class A
{
    public A()
    {
        Console.WriteLine("Base Constructor");
    }
    public void display()
    {
        Console.WriteLine("Base Method");
    }
}
class B:A
{
    public B()
    {
        Console.WriteLine("Derived Constructor");
    }
    public void display()
    {
        Console.WriteLine("Derived method");
    }
}
       
class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.display();
            Console.ReadLine();
        }
    }


Ø Multilevel inheritance(derived from a derived class)


   A common requirement in object-oriented programming is the use of a derived class as a super class. This concept allows us to build a chain of classes as shown in fig.

   The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The chain ABC is known as inheritance path.



   This process may be extended to any number of levels. The class C can inherit the members of both A and B. the constructor are executed from the top downwards, with the bottom most class constructor being executed last.

   Care should be taken to place arguments in proper order so that correct values are passed to the data members. Below program shows the multilevel inheritance.

using System;

class A
{
    public A()
    {
        Console.WriteLine("Base Constructor");
    }
    public void display()
    {
        Console.WriteLine("Base Method");
    }
}
class B : A
{
    public B()
    {
        Console.WriteLine("Intermediate Constructor");
    }
    public void display()
    {
        Console.WriteLine("Intermediate Method");
    }
}
class C : B
{
    public C()
    {
        Console.WriteLine("Derived Constructor");
    }
    public void display()
    {
        Console.WriteLine("Derived Method");
    }
}
class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        b.display();
        C c = new C();
        c.display();
        Console.ReadLine();
    }
}
   Output:-

Base Constructor
Intermediate Constructor
Intermediate Method
Base Constructor
Intermediate Constructor
Derived Constructor
Derived Method


Ø Hierarchical inheritance(one base class, many subclasses)


   Another interesting application of inheritance is use it as a support to use it as a support to the hierarchical design of a program. Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below the level.

   An example fig shows a hierarchical classification of accounts in a commercial bank. This is possible because all the accounts possess certain common features.













   Below program shows the hierarchical inheritance.








using System;

class A
{
    public A()
    {
        Console.WriteLine("Base Constructor");
    }
    public void display()
    {
        Console.WriteLine("Base Method");
    }
}
class B : A
{
    public B()
    {
        Console.WriteLine("Chiled - 1 Derived
Constructor");
    }
    public void display()
    {
        Console.WriteLine("Chiled - 1 Derived Method");
    }
}
class C : A
{
    public C()
    {
        Console.WriteLine("Child-2 Derived Constructor");
    }
    public void display()
    {
        Console.WriteLine("Child-2 Derived Method");
    }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        b.display();
        C c = new C();
        c.display();
        Console.ReadLine();
    }
}

   Output:-
Base Constructor
Chiled - 1 Derived Constructor
Chiled - 1 Derived Method
Base Constructor
Child-2 Derived Constructor

Child-2 Derived Method
Next
This is the most recent post.
Older Post

0 comments:

Post a Comment

Subscribe us