Week5 InheritancePolymorphismAbstraction PDF
Week5 InheritancePolymorphismAbstraction PDF
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.
Syntax:
Here’s an example:
class Vehicle //superclass(base)
{
public string brand; // Vehicle field
public void honk() // Vehicle method
{
Console.WriteLine(" ");
}
}
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.
Access modifier:
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.
Syntax:
public ExtendingClass() { … }
}
Here’s an example of the above scenario:
class Shape
{
protected int noOfsides;
protected string dimension;
// no constructor
//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:
}
Here’s an example:
//constructor
public Shape(int noOfsides, string dimension) {
this.noOfsides = noOfsides;
this.dimension = dimension;
}
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 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 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.
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");
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).
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:
Only declare a class abstract if you have an abstract method for the class.
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
//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;
}
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)
<=10000 3% 2% 5% 1%
>20000&<=30000 7% 7% 15% 5%
***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)
***Define in main which tests the functionality of the 2 derived classes. The main class works as follows:
MAIN MENU
[1] Doctor
[2] Programmer
[3] Exit
Doctor’s Information:
ID 001
Specialization Pediatrician
Salary 15750.00
Deduction 3386.25
Programmer’s Information:
ID 001
Specialization Pediatrician
Salary 14250
Deduction 2707.50