You are on page 1of 18

Inheritance

 Inheritance is an important feature of OOP (Object Oriented Programming).


 It is the mechanism in C# by which one class is allowed to inherit the features (fields and
methods) of another class.

Different types of Inheritance

Single inheritance

 It is the type of inheritance in which there is one superclass and one subclass.

Class A is the parent class and Class B is the child class since Class B inherits the features
and behavior of the parent class A.

Hierarchical inheritance

 This is the type of inheritance in which there are multiple classes derived from one
superclass. This type of inheritance is used when there is a requirement of one class
feature that is needed in multiple classes.
Class A has two childs class B and class D. Further, class B and class C both are having two
childs - class D and E; class F and G respectively.

Multilevel inheritance

 When one class is derived from another derived class then this type of inheritance is
called multilevel inheritance.

Class c inherits the properties and behavior of class B and class B inherits the properties
and behavior of class A. So, here A is the parent class of B and class B is the parent class
of C. So, here class C implicitly inherits the properties and behavior of class A along with
Class B i.e there is a multilevel of inheritance.

Multiple inheritance

 C# does not support multiple inheritances of classes, so overcome this problem C# can
use interfaces

Basic Terms

Superclass - the class whose features are inherited is known as super class (base or a parent class).

Subclass - the class that inherits the other class is known as subclass (derived, extended c, or child class).
The subclass can add its own fields and methods in addition to the superclass fields and methods.

Why use inheritance?

 Inheritance supports the concept of “reusability”


 Inheritance “reduce” complexity in code
How to implement Inheritance?

To inherit from a class, use the : symbol.

Syntax:

class subclass : superclass

{ // fields, properties, methods .

Here’s an example:
class Vehicle //superclass(base)
{
public string brand; // Vehicle field
public void honk() // Vehicle method
{
Console.WriteLine(" ");
}
}

class Car : Vehicle // subclass (derived)


{
public string modelName = ; // Car field
}

Another example:
class Teacher //superclass(base)
{
public void Teach()// Teacher method
{
Console.WriteLine("Teach");
}
}
class Student : Teacher // subclass (derived)
{
public void Learn()()// Student method
{
Console.WriteLine("Learn");
}
}

Highlighted in yellow color shows how to implement inheritance. In the above example class Car inherit
class Vehicle. Also, class Student inherit class Teacher.
The subclass (derived) class has all the features of the superclass and can add some new features and it
also depends on the access modifier that is used at the time of superclass inheritance.

Implementing inheritance will use either public or protected access modifier.

Access modifier:

Constructors with Inheritance

In C#, both the superclass and the subclass can have their own constructor.

The constructor of a base class used to instantiate the objects of the base class and the constructor of
the derived class used to instantiate the object of the derived class.

In inheritance, the derived class inherits all the members (fields, methods) of the base class, but derived
class cannot inherit the constructor of the base class because constructors are not the members of the
class.

Instead of inheriting constructors by the subclass, it is only allowed to invoke the constructor of
superclass.

Two scenarios when working constructors for inheritance

1. Only derived class contains a constructor


 Objects of the derived class are instantiated by that constructor and the objects
of the base class are instantiated automatically by the default constructor.

Syntax:

public class ExtendingClass : BaseClass

public ExtendingClass() { … }

}
Here’s an example of the above scenario:

Superclass: no constructor, default constructor will be use

class Shape
{
protected int noOfsides;
protected string dimension;

// no constructor

Subclass: with constructor


class Triangle:Shape
{
protected string type;

//construtor
public Triangle(int noOfsides, string dimension, string type) {
// superclass
this.noOfsides = noOfsides;
this.dimension = dimension;
// subclass
this.type = type;
}
}

In the above example, Shape is the superclass and Triangle is the subclass. Shape class provides the no
of sides and dimension of the Shape and Triangle provides the type of triangle. And Shape class does not
contain any constructor so the default constructor is used to instantiate the object of a class and
Triangle class contains Triangle() constructor which instantiate the object of a class.

2. Both the base class and derived class has their own constructors
 C# provide a keyword known as a base keyword, with this the derived class can
call the constructor which is defined in its base class.

Syntax:

public class ExtendingClass : BaseClass

public ExtendingClass() : base() { … }

}
Here’s an example:

Superclass: with constructor


class Shape
{
protected int noOfsides;
protected string dimension;

//constructor
public Shape(int noOfsides, string dimension) {
this.noOfsides = noOfsides;
this.dimension = dimension;
}

Subclass: with constructor

class Triangle:Shape
{
protected string type;

//construtor
public Triangle(int noOfsides, string dimension, string
type):base(noOfsides,dimension) {
// subclass
this.type = type;
}

In the above example, Shape is the super class and Triangle is the subclass. The Shape class describes the
dimension, and the no of side of the shape. The Triangle describe the type of triangle. Both the
superclass and the subclass have their own constructor. But we declare the constructor of Triangle with
a base keyword as shown in the highlighted yellow color of the above code.

Here Triangle() call base class constructor with the parameter noOfsides and dimension. That means
Shape() constructor is called and it will initialize the value of noOfsides and dimension in Triangle (). So
there is no need for Triangle class to initialize these values. If Triangle required an extra field, then the
field should be unique from the called fields like type.

By using the base keyword, it becomes easier to initialize the objects of the superclass without any
conflict and it also provides an authority to call the constructor of a superclass from the subclass and
also save the time of re-writing the codes.
NOTE:

Any form of the constructor defined in the superclass can be called by the base keyword, but only that
constructor executes that matches the arguments.

If a class has private constructors only, then it cannot be inherited.

If a class has private constructors only, then this could indicate many other things.

For example, no-one (other than that class itself) can create instances of such a class.

Actually, that’s how one of the most popular design patterns (Singleton) is implemented.

Methods (Inheritance)
Methods from superclass can be inherited and use by its subclass. However, some methods can be
implemented differently though the method name is the same. This is the concept of polymorphism.

What is Polymorphism?

 Polymorphism means providing an ability to take more than one form


 polymorphism is a combination of two words, one is poly and another one
is morphs.
 poly means “multiple” and morphs means “forms” so polymorphism means
many forms.

Polymorphism provides an ability for the classes to implement different methods that are called
through the same name and it also provides an ability to invoke the methods of a subclass through
superclass reference during runtime based on our requirements.

Two types of Polymorphism

1. Compile Time Polymorphism


2. Run Time Polymorphism

Compile Time Polymorphism means defining multiple methods with the same name but with different
parameters. This can be achieved by using method overloading, sometimes called early binding or
static binding.

Here is an example:
public int sum(int a, int b)
{
return a + b;
}
public int sum(int a, int b, int c)
{
return a + b + c;
}
Run Time Polymorphism means overriding a superclass method in the subclass by creating a similar
method. This can be achieved by using method overriding, sometimes called late binding or dynamic
binding.

Here is an example:
class Shape
{
public virtual void display()
{
Console.WriteLine("Superclass");
}
}

class Triangle:Shape
{
public virtual void display()
{
Console.WriteLine("Subclass");
}

Now, let’s summarize the above discussion in the sample code below:

//superclass
class Shape
{
protected int noOfsides;
protected string dimension;
//constructor
public Shape(int noOfsides, string dimension)
{
this.noOfsides = noOfsides;
this.dimension = dimension;
}
//properties
public void setNoOfsides(int noOfsides) {
this.noOfsides = noOfsides;
}
public void setDimension(string dimension) {
this.dimension = dimension;
}
public int getNoOfsides(){
return noOfsides;

}
public string getDimension() {
return dimension;
}
// methods
public void display() {
Console.Write("Number of sides:" + getNoOfsides() + "\n Dimesion" +
getDimension());
}

}
//superclass
class Triangle:Shape
{
protected string type;
protected int b;
protected int h;

//construtor
public Triangle(int noOfsides, string dimension, string type, int b, int h):
base(noOfsides, dimension)
{
// subclass
this.type = type;
this.b =b;
this.h = h;
}
//properties
public void setType(string type) {
this.type = type;
}
public string getType() {
return type;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
public void setH(int h) {
this.h = h;
}
public int getH() {
return h;
}
//method overriding (example of polymorphism)
//same method name different implementation
public void display()
{
// this is to inherit the display method (implementation) of superclass
base.display();
Console.Write("\nType of triangle: " + getType());
}

}
static void Main(string[] args)
{
Shape s;
Triangle t;
//creat shape object
s = new Shape(4,"3D");
//create triangle object
t = new Triangle(3,"2D","right triangle");

// calling method of superclass


Console.Write("\nDetails of Shape object:");
s.display();
Console.Write("\n");
// calling method of subclass
Console.Write("\nDetails of Triangle object:");
s.display();

Console.ReadKey();

Output:
Now, let’s have Abstraction, and how it is related to inheritance.

Abstraction

 is the process of hiding certain details and showing only essential information to the user.
 Abstraction can be achieved with either abstract classes or interfaces
 abstract keyword is used for classes and methods:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body is
provided by the derived class (inherited from).

Abstract class can have both abstract and regular methods.

How to implement abstract class?

To create an abstract class, abstract keyword is use.

Here is an example:
//superclass
abstract class Shape
{
// abstract method in superclass
public abstract double area();

}
//subclass
//subclass must implement abstract method of superclass)
// replace abstract with override
public override double area(){

Note:

You cannot create an instance of an abstract class.

You cannot declare an abstract method outside an abstract class.

Only declare a class abstract if you have an abstract method for the class.

Abstract methods of superclass must have an implementation in the subclasses.

When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.

Also, inheritance can be implemented without using abstract classes and methods.
Here’s the sample code of an abstract class with the implementation of inheritance

Let’s modify the above sample code:

//superclass
// to create an abstract class
abstract class Shape
{
protected int noOfsides;
protected string dimension;
//constructor
public Shape(int noOfsides, string dimension)
{
this.noOfsides = noOfsides;
this.dimension = dimension;
}
//properties
public void setNoOfsides(int noOfsides) {
this.noOfsides = noOfsides;
}
public void setDimension(string dimension) {

this.dimension = dimension;
}
public int getNoOfsides(){
return noOfsides;

}
public string getDimension() {
return dimension;
}

// methods
public void display() {
Console.Write("\nNumber of sides: " + getNoOfsides() + "\nDimesion: " +
getDimension());
}
// abstract method
public abstract double computeArea();

//subclass
class Triangle:Shape
{
protected string type;
protected int b;
protected int h;
//construtor
public Triangle(int noOfsides, string dimension, string type, int b, int h):
base(noOfsides, dimension)
{
// subclass
this.type = type;
this.b =b;
this.h = h;
}
//properties
public void setType(string type) {
this.type = type;
}
public string getType() {
return type;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
public void setH(int h) {
this.h = h;
}
public int getH() {
return h;
}
//method overriding (example of polymorphism)
//same method name different implementation
public void display()
{
// this is to inherit the display method (implementation) of superclass
base.display();
Console.Write("\nType of triangle: " + getType());
}
//implementation of abstract method in the superclass
//use override keyword to replace abstract
public override double computeArea()
{
double area;
area = (getB() * getH()) / 2;
return area;
}

static void Main(string[] args)


{
//No Instance for shape since it is an abstract class
Triangle t;
//create triangle object
t = new Triangle(3,"2D","right triangle" , 6,10);
Console.Write("\n");
// calling method of subclass
Console.Write("\nDetails of Triangle object:");
t.display();
Console.Write("\nArea of triangle:" + t.computeArea());
Console.ReadKey();

Output:

Practice exercise

Create a subclass Square. Have an instance of the class, and display its details. Also, implement a
method that will compute the area and perimeter of a square.

Exercises (Graded)

*** Define an abstract class named Employee which has the following data members:

 integer employee id
 string name
 double salary
 double deduction
and methods:

 2 constructors (one with no parameter and the other accepts 2 parameters – employee
id and name)
 Setter/getter methods for each data member
 display method which displays the 3 data members (id,name,salary)
 abstract calculateSalary which computes the salary of the employee and returns a double
value (this method accepts one parameter of type double which represents the number
of days worked in a month)
 abstract calculateDeduction which computes the total deductions of an employee in a
month and returns a double value (this method does not accept any parameter)
 abstract calculateNetPay which computes the net pay of an employee and returns a
double value. This method does not accept any parameter.

***Define a class for Doctor which is a derived class of employee. This class defines one property which
is String specialization. The methods of this class include:

 a constructor which accepts 3 parameter (2 from Employee and String specialization) and
must invoke the constructor of the parent class and sets appropriate values
 setter/getter method for the specialization data member
 an overloaded display method which displays the employee id, name, salary and field of
specialization of the doctor
 an overridden calculateSalary method which computes the salary of an employee based
on the following specifications:
Field of Specialization Daily Rate

Pediatrician 2050

Ob-Gynecologist 2650

Neurologist 6575

Salary is calculated by multiplying the daily rate by the numbers of days worked in a
month. Set the salary of the employee to the value computed. (In this part, invoke the
setSalary method of the parent class)

 an overridden calculateDeduction method which computes the deduction of an employee


in a month based on the salary computed. To start the calculation, call the method
calculateSalary and store the value to a variable of type double. The deductions are based
on the following specifications:
Salary SSS Pag-ibig WTAX PhilHealth

<=10000 3% 2% 5% 1%

>10000&<=20000 5% 4% 10% 2.5%

>20000&<=30000 7% 7% 15% 5%

>30000 10% 10% 30% 8%

***Define a class Programmer which is a derived class of Employee. This class defines one property of
type string which represents the programming language specialized by the programmer. The methods of
this class include:

 2 constructors (one with no parameters and the other accepts 3 parameters, 2 of which
are to be passed to the constructor of the parent class)
 setter/getter method for the data member
 an overridden display method which displays the employee id, name, salary and field of
specialization of the doctor
an overridden calculateSalary method which calculates salary by multiplying the days
worked 950 as the daily rate. Set the salary of the employee to the value computed. (In
this part, invoke the setSalary method of the parent class)

 an overridden calculateDeduction method which computes deduction based on the


following:
o SSS 5% of the salary
o Pag-ibig 3% of the salary
o WTAX 8% of the salary
o PhilHealth 3% of the salary
 an overridden calculateNetPay which computes the net pay of an employee by
subtracting the total deductions from the salary computed.

***Define in main which tests the functionality of the 2 derived classes. The main class works as follows:

 Create an object for the class Doctor and Programmer


 In each class, ask the user for inputs which corresponds to the data members for each
object. For instance, for the class Doctor, ask for 5 user inputs which represent employee
id, name, specialization and the number of days worked in a month. The first 3 parameters
are passed to the constructor upon creating the object. The number of days worked is the
parameter needed for the calculateSalary method.
 Invoke the display method of each class to display all information about the employee
 Sample output is shown below:

MAIN MENU

[1] Doctor

[2] Programmer

[3] Exit

If the user chooses 1, the sample output is:

Enter ID Number : 001

Enter Name : Juan dela Cruz

Enter Field of Specialization : Pediatrician

Number of days worked : 15 <NOTE: days must not exceed 30 days>


//in this part, the display method is invoked

Doctor’s Information:

ID 001

Name Juan dela Cruz

Specialization Pediatrician

Salary 15750.00

Deduction 3386.25

Net Pay 12363.75

If the user chooses 2, the sample output is

Enter ID Number : 001

Enter Name : Juan dela Cruz

Enter Field of Specialization : Java

Number of days worked : 15 <NOTE: days must not exceed 30 days>

//in this part, the display method is invoked

Programmer’s Information:

ID 001

Name Juan dela Cruz

Specialization Pediatrician

Salary 14250

Deduction 2707.50

Net Pay 11542.50

You might also like