11 - Class Inheritance in C#

11.1 Identifying relationship between Classes

In Object Oriented Programming, the classes and objects are related to each other. The classes and objects react with other components in an application. The behavior of the object is shown by the action it performs in response to the messages sent by other object.

In OOP, the following relationship can be established between the objects of different classes are mentioned below:

1) Inheritance Relationship

2) Composition Relationship

3) Utilization Relationship

4) Instantiation Relationship

1) Inheritance Relationship

In OOP, we use classes to inherit commonly used state and behavior from other classes. Consider an example of automobile. Automobile is the super class of car and a bus. Car and bus inherits attributes and behavior from automobile. The car, bus and automobile have a relationship where one object is related to another.

Superclass and Subclass

The superclass is the generalization class. It means that multiple classes can inherit certain attributes from the same superclass. It is used to create programs that can be changed according to the new requirements. Reusability can be achieved using generalization. It is implemented using an abstract superclass.

Consider an example of superclass and subclass in C#. The following figure shows the hierarchy of superclass and subclass in C#.

In the above example, the Car and Bus classes inherit the characteristics of the Automobile class. The Automobile class is the superclass of the Car and Bus classes as its subclass.

Sometimes, user does not want to create an object of the Automobile class in the program. Instead, he wants to use it only as a superclass for the Car and Bus classes. Here, Automobile is the abstract superclass of the subclasses Car and Bus.

2) Composition Relationship

OOP helps user to form an object that includes another object as its part. This mechanism of forming object is called composition. It is used for objects that have a ‘has-a’ relationship with each other.

Consider an example of car. A car has an engine. This type of relationship between the car and the engine is known as composition relationship.

3) Utilization Relationship

OOP allows a class to make use of another class. This kind of relationship is known as utilization. Consider an example of utilization relationship. BMW is an object of the car class, and Patrick is an instance of a driver. Patrick drives a BMW. Driver James drives a bus. Hence, both car and bus class use the driver class. This type of relationship between a driver and a bus or a driver and a car is known as utilization relationship.

4) Instantiation Relationship

An instantiation relationship is a relationship between a class and an instance of that class. Some of the examples of instantiation relationship are as follows:

1) A BMW is a car

2) A chair is a type of furniture

3) Jennifer is a student

11.2 Implementing Interfaces

Each instance of a derived class includes its own attributes and all the attributes of the base class. The attributes in derives class are larger sets as compared to the base class. The derived class can modify some of all the attributes. It avoids redundancy in code and enables easy maintenance of the code. Changes to the base class changes the behavior of the subclasses.

The syntax for the creation of derived class in C# is as shown below:

<access-specifier>class <base_class>
{
….
}

class <derived_class> : <base_class>
{
….
}

The code snippet for implementing inheritance is as shown below:

class Employee
{
    int noofdays;
    int nonleavedays;
    public void Calc_Leave()
    {
        Console.WriteLine(“Enter the number of working days”);
        noofdays = Convert.ToInt32(Console.ReadLine());
        nonleavedays = noofdays – 4;
        Console.WriteLine(“No of non leave days are:{0}”, nonleavedays);
    }
}
class Manager: Employee
{
    public void leave()
    {
        Calc_Leave();
    }
    static void Main ( string[ ] args )
    {
        Employee e = new Employee();
        e.Calc_Leave();
        Console.Read();
    }
}

 

The output for the code is as shown below:

Base class Initialization

The base class can be initialized by calling the constructor of the base class when creating an instance of the derived class. The base keyword is used to access the methods and properties of the base class from the method in the derived class.

The following code shows the base keyword in C#.

class Person
{
    protected string name = “John”;
    protected int age = 20;
    
    public virtual void GetData()
    {
        Console.WriteLine(“Name is :{0}”, name);
        Console.WriteLine(“Age is: {0}”, age);
    }
}
class Employee:Person
{
    public string empno = “1”;
    public override void GetData()
    {
        base.GetData();
        Console.WriteLine(“Emp Id is :{0}”, empno);
    }
}
class Test
{
    static void Main ( string[ ] args )
    {
        Employee e = new Employee();
        e.GetData();
        Console.Read();
    }
}

 

The output for the code is as shown below:

11.3 Abstract class

Abstract class states that the class is incomplete and cannot be used directly. Abstract class contains abstract methods, which can be implemented in the derived class. Instance of the abstract class cannot be created for using the member functions. You can override the methods of the abstract class in the base class and add a new functionality.

An abstract class defines only abstract variables and methods in their implementation in the derived class. An abstract class is defined by using abstract keyword before the class keyword.

The following code shows the declaration of an abstract class in C#.

abstract class classname
{
    //This is abstract class
}
class TestAbstract
{
    static void Main ( string[ ] args)
    {
    }
}

 

In the above code, the abstract class is defined using the abstract keyword. The rules for the use of an abstract class are as shown below:

1) User cannot create an instance of an abstract class

2) User cannot declare an abstract method outside an abstract class

3) User cannot declare an abstract method as sealed

4) A class derived from the abstract class must override all the methods of the abstract class

5) If the derived class cannot implement all the methods of the abstract class, then the derived class must also be defined as abstract

Abstract methods are the methods without any body. When a derived class inherits the abstract method from the abstract class, it must override the abstract method provides the new functionality. The requirement is enforced at the compile time, and it is known as dynamic polymorphism.

The syntax for using the abstract method is:

[access-specifiers] abstract return-type method-name ( [ parameters] );

The following code shows the use of abstract class with abstract methods:

abstract class Animal
{
    public abstract void FodHabits();
}
class Herbivours : Animal
{
    public override void FoodHabits();
    {
        Console.WriteLine(“They eat only plants”);
    }
}
class Carnivours: Animal
{
    public override void FoodHabits();
    {
        Console.WriteLine(“They eat animals”);
    }
} 
class Implement
{
    static void Main ( string[] args)
    {
        Carnivours c = new Carnivours();
        Herbivours h = new Herbivours();
        h.FoodHabits();
        c.FoodHabits();
        Console.Read();
    } 
}

 

The output for the code is as shown below:

11.4 Virtual Functions

A function defines in a class can be implemented into the inherited classes through virtual functions. The functionality of the virtual function can be modified by the inherited class according to the requirements. The virtual keyword is used to declare the virtual function in C#. When the virtual function is used in the derived class, the override modifier is used. When overriding a method the type signature of the override method must be same as the virtual method. The virtual function cannot be static or abstract.

The code snippet for the virtual function in C# is as shown below:

class Calculation
{
    public virtual void Operations()
    {
        Console.WriteLine(“Perform specific operations”);
    }
}
class Addition:Calculation
{
    public override void Operations()
    {
        Console.WriteLine(“the addition is performed”);
    }
}
class Subctraction:Calculation
{
    public override void Operations()
    {
        Console.WriteLine(“The substraction is performed”);
    }
}
class Implement
{
    static void Main ( string[ ] args )
    {
         Addition a = new Addition();
        Subctraction s = new Subctraction();
        a.Operations();
        s.Operations();
        Console.Read();
    }
}

The output for the code is as shown below:

11.5 Sealed classes

When a user does not need to extent the class in an application, the sealed keyword is used. It tells the compiler that the class is sealed and it cannot be extended. User cannot derive the class from a sealed class.

The following is an example of the sealed class:

sealed class Program
{
    public int a;    
    static void Main( string[ ] args)
    {
    }
} 

A method can be sealed, where the method cannot be overridden. The following is an example of using a sealed method in a sealed class,

sealed class Program
{
    public int a;
    public sealed void Add()
    {
    }
    static void Main ( string[ ] args )
    {
    }
}

 

Like us on Facebook