Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

Inheritance

Inheritance relationship exists when there is a parent child relationship.

In inheritance generally there will be two types of classes

1. Base Class(Parent Class)

It is a class which provides its features to derived class.

2. Derived Class(Child class)

It is a class which takes features from base class.

** In C# we represent inheritance using colon(:) symbol

class Derived : Base // syntax of inheritance

** C# does not support multiple inheritance.

A derived class cannot inherit from more than one base class at a time.

class Derived : Base1 // syntax of inheritance

** Here will learn the implementation f proteceted access specifier., which comes
into picture in case of inheritance

** protected members of the base class are accessible only to the derived class.

using System;
class A // internal
{

protected string ="ayush";

class B : A //due to this line compiler error will generate

public string city = "agra";


}

public static void Main()


{

B v = new B();
Console.WriteLine(v.city);
Console.WriteLine(v.name);

}
}

** Derived class cannot have higher accessibility level than the base class.
** In case if your derived class is having higher accessibility level than the base
class, you will get compiler error,

** Inheritace is a way to implement resuability in our applications.

** In C#, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically.

** In C#, the class which inherits the members of another class is called derived
class and the class whose members are inherited is called base class. The derived
class is the specialized class for the base class.

1. Single Level inheritance with fields

when one class inherits another class, it is known as single level inheritance .

using System;

class Employee //base class


{
protected float salary =40000;

class Supervisor : Employee //supervisor is the derived class


{

public float incentives = 5000;

public static void Main()


{

Supervisor s new Supervisor();


Console.WriteLine("Salary is " + s.salary);
Console.WriteLine("Incentives is " + s.incentives);
}
}

** Inheritance represents Is-A relationship

Ram is a humanBeing

chair is a furniture

Manager is a employee

Cow is a animal

Sigle level inheritance with Methods

using System;
class Animal //base class
{
private void eat()
{
Console.WriteLine("Eating...");
}

class Dog : Animal //Dog is a derived class of anima.


{
public void bark()
{
Console.WriteLine("Barking....");
}
}
class Test //driver class
{
Dog d = new Dog();
d.eat();
d.bark();
}
}

write a program where customer will be a paret class and it is having one method
called Display " Hello i am customer" which is protecetd and there will be a
derived class called RoyalCustomer which is a derived class and it is having its
own method displayRoyal whch should prit message "i am a royal customer".

Miutli Level Inheritance

When one class inherits another class which is further inherited by another class,
it is known as multi level inheritance in C#

using System;
class Animal //base class
{
protected void eat()
{
Console.WriteLine("Eating...");
}

class Dog : Animal //Dog is a derived class of anima.


{
public void bark()
{
Console.WriteLine("Barking....");
}

class babydog : Dog


{
public void weep()
{
Console.WriteLine("weeping");
}
public static void Main()
{
babydog bd = new babydog();
bd.weep();
bd.bark();
bd.eat();
}
}

Constructor calling sequence in case of constructor

** The sequence of execution of constructor in case of inheritance is from Base to


Derived .

autoatically even if you only create an object of derived class, then also first
the base class constructor executes and then derived class.

simple example

using System;
class MyBase

public MyBase()
{
Console.WriteLine("this is base class constructor");
}

~ MyBase()
{
{
Console.WriteLine("this is base class destructor");
}

class MyDerived : MyBase


{

public MyDerived()
{
Console.WriteLine("This is derived class constructor");
}

~ MyDerived()
{
Console.WriteLine("This is derived class destructor");
}

public static void Main()


{
MyDerived md = new MyDerived();
}
}

this is base class constructor


This is derived class constructor
This is derived class destructor
This is base class destructor

** In case of destructor , it executes from derived to base

base

It is a keyword which is used to access base class variables and functions from
derived class.

In C#, base keyword is used to access fields(variables), constructors and methods


of base class from derived class.

using System;

class Animal
{
public string color = "Blue";

class Dog : Animal


{

string color = "Black";

public void showcolor()


{

Console.WriteLine(base.color); //calling base class member


Console.WriteLine(color);
}
public static void Main()
{
Dog d = new Dog();
d.showcolor();
}
}

Blue
Black

Example to access methods of base class using base keyword

using System;
class Rectangle
{
public void draw()
{
Console.WriteLine("drawing rectangle");
}
}
class circle : Rectangle
{
public void draw()
{
base.draw(); // is calling the base class draw method
Console.WriteLine("Drawing circle");
}
public static void Main()
{
circle c = new circle();
c.draw();

}
}

drwaing rectangle
Drawing circle

** Protected members
A protected method of a base class cannot be accessed using the base class object
from derived class

Method overloadiing was an example of static polymorphism

Method overriding is an example of Dynamic polymorphism

It is a concept of redefining the methods of the base class in derived class.

some keywords used to perform Method overriding

1. virtual = It is a keywords which is used with only base class methods

2. override = This keyword is used with derived class methods.

ovevride keyword is used to modify a virtual or abstract function.

** If a derived class defines the same method as of the base class, then we call it
as method overriding,

** To perform method overriding in C#, you need to use virtual keyword with base
class method and override keyword with derived class method

using System;
class Animal
{
public virtual void eat()
{
Console.WriteLine("Thsi is super class eat method");
}
}

class Dog : Animal


{
public override void eat()
{
Console.WriteLine("eating bread");
}
public static void Main()
{
Dog d = new Dog();
d.eat();
}
}

using base keyword to call base class constructor


using System;

class A
{
public int n1, n2;
public A()
{
Console.WriteLine("Default Constructor");
}

public A (int i , int j)


{
n1 = i;
n2 = j;
Console.WriteLine("parameteried constructor executed");
Console.WriteLine("Values are : " + n1+ "and" + n2);
}
}
class MyDerived : A
{
//Here we are calling base class constuctor using base keyword
public MyDerived() : base()
{
}
public MyDerived(int inj , i) : base (i,j)
{
}
static void Main()
{
MyDerived a = new MyDerived();
MyDerived aa = new MyDerived(10,20);
}
}

You might also like