Professional Documents
Culture Documents
28th Feb Notes
28th Feb Notes
A derived class cannot inherit from more than one base class at a time.
** 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
{
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,
** 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.
when one class inherits another class, it is known as single level inheritance .
using System;
Ram is a humanBeing
chair is a furniture
Manager is a employee
Cow is a animal
using System;
class Animal //base class
{
private void eat()
{
Console.WriteLine("Eating...");
}
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".
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...");
}
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");
}
public MyDerived()
{
Console.WriteLine("This is derived class constructor");
}
~ MyDerived()
{
Console.WriteLine("This is derived class destructor");
}
base
It is a keyword which is used to access base class variables and functions from
derived class.
using System;
class Animal
{
public string color = "Blue";
Blue
Black
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
** 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 A
{
public int n1, n2;
public A()
{
Console.WriteLine("Default Constructor");
}