Inheritance and Interfaces - C# FOR BEGINNERS CRASH COURSE (2014)

C# FOR BEGINNERS CRASH COURSE (2014)

Chapter 12 Inheritance and Interfaces

12.1 Introduction to Inheritance

The concept of inheritance is fundamental to not only C# but also to most other programming languages. It allows the user to create classes that inherit data members and methods from other classes. Inheritance can save the user time by allowing them to create several popular and well-used methods in a core class and have other classes inherit that core class and all its methods. This stops the user from repeating the same method in every class.

12.2 Base and derived classes

The structure of inheritance can be summed up in a few ways, for this example we have three classes A, B and C. The class B is derived from the class A. The class C is derived from class B. So what does this mean?

If we symbolize the relationship of these classes then we could show it like this:

Class Aß Class B ß Class C

· Since class B is derived from class A, we say that class A is the parent of class B. We can also say that class B is the child of class A.

· Since class C is derived from class B, we say that class B is the parent of class C. We can also say that class C is the child of class B.

· Since class C is derived from class B, which in turn is derived from class A, we say that class A is the grandparent of class C. We can also say that class C is the grandchild of class A.

· In this instance since class A does not inherit from other classes, this class would be called the base class. Class B and class C would be declared as derived classes.

In this instance we have used the metaphor of parent and child to represent the relationship between the different classes but you will also encounter other metaphors such as super class (base class) and sub classes (derived classes) that describe the same relations.

The following is the way we would declare these relationships in code:

<access – specifier> class <base_class>

{

…..

}

class <derived_class> : <base_class>

{

…..

}

Example:

Example 46:

using System;

namespace inheritance1

{

class Demo

{

public void side(int s)

{

side = s;

}

protected int side;

}

//Derived class

class Area : Demo

{

public int getAreaOfSquare()

{

return s * s;

}

}

clas Program

{

static void Main(string[] args)

{

Area a = new Area();

a.side(4);

//Display the area of square

Console.WriteLine("Area is: {0}",a.getAreaOfSquare());

Console.Read();

}

}

}

When the code is compiled and executed, the output is:

Area is: 16

12.3 Base class initialization

When creating an inheritance structure the user must start with the base class (super class) first. Below is an example of how a base class is set up and how derived classes inherit from it.

Example:

Example 47:

using System;

namespace Base1

{

class Square

{

//member variables

protected int side;

public Square(int s)

{

side = s;

}

public int CalculateArea()

{

return side * side;

}

public void Show()

{

Console.WriteLine("The value of side is:{0}",side);

Console.WriteLine("Area is:{0}",CalculateArea());

}

}

class Paint : Square

{

private int amount;

public Paint(int side):base(s)

{

}

public int Totalamount()

{

int amount;

amount = CalculateArea * 50;

return amount;

}

public void Show()

{

base.Show();

Console.WriteLine("Total amount is:{0}",Totalamount());

}

}

class Program

{

static void Main(string[] args)

{

Paint p = new Paint(7);

p.Show();

Console.Read();

}

}

}

When the code is compiled and executed, the output is:

The value of side is: 3

Area is: 9

Total amount is: 450

12.4 Interfaces in C#

An interface is an abstract base class that declares methods but do not declare any logic within the methods. Essentially interfaces can be described as contracts, which all classes that inherit it the interface must follow. Interfaces spell out what methods and attributes every derived class must have. If these methods are not present in the derived classes then an error will occur.

Declaring Interfaces

The interface keyword is used for declaring the interface. They have public as their default data type.

Example of interface declaration

public interface IInterface1

{

void MethodToImplement();

}

Example:

Example 48:

using System;

namespace interface1

{

public interface IStudentInfo

{

//interface members

void ShowData();

}

public class StudentInfo : IStudentInfo

{

private int srno;

private string name;

private string subject;

public StudentInfo()

{

srno = "";

name = "";

subject = "";

}

public StudentInfo(int s, string n, string b)

{

srno = s;

name = n;

subject = b;

}

public void ShowData()

{

Console.WriteLine("Student no is:{0}",srno);

Console.WriteLine("Student name is:{0}",name);

Console.WriteLine("Subject is:{0}",subject);

}

}

class Program

{

static void Main(string[] args)

{

StudentInfo s = new StudentInfo(10,"Alex","Maths");

StudentInfo s1 = new StudentInfo(20,"Adam","Science");

s.ShowData();

s1.ShowData();

Console.Read();

}

}

}

When the code is compiled and executed, the output is:

Student no is: 10

Student name is: Alex

Subject is: Maths

Student no is: 20

Student name is: Adam

Subject is: Science

12.5 Multiple inheritance in C#

Multiple inheritance is the practice of derived classes inheriting from two unrelated classes. This practice is not naturally supported in C#, however with the use of interfaces we can mimic the behavior of multiple inheritance to a certain degree.

Below is an example of this use of interfaces to simulate multiple inheritance:

Example 49:

using System;

namespace multiple1

{

class Area

{

protected int base;

protected int height;

public void setbase(int b)

{

base = b;

}

public void setheight(int h)

{

height = h;

}

}

//Base class

public interface Paint

{

int amount(int area);

}

//Derived class

class Triangle : Area,Paint

{

public int getValue()

{

return ( 1/2 * b * h );

}

public int amount(int area)

{

return area * 50;

}

}

class Program

{

Triangle t = new Triangle();

int area;

t.setbase(4);

t.setheight(5);

area = t.getValue();

//Display the area

Console.WriteLine("Area is:{0}",t.getValue());

Console.WriteLine("Total Paint cost is:{0}",t.amount());

Console.Read();

}

}

When the code is compiled and executed, the output is:

Area is: 10

Total Pain cost is: 500