Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them. Inheritance is almost like embedding an object into a class.
1. It is the process by which a class can reuse and extend the existing class without modifying them.
2. It is like embedding an object into a class.
3. It is a default association of base class in the derived class.
4. It gives us the ability to include the name and definition of the members into a new class. New class has or contains sub-object of base type.
5. It provides a mean of encapsulation.
6. We don't need to create the object of base class in the derived class to call it's functions in the derived class because the memory allocation of base class is implicitly took place in the derived class.
7. Without inheritance reusability is also achieved but a class don't get the behavior of a class.
Example: -Without using Inheritance
using System;
class A
{
int data;
public void f(int arg)
{
data = arg;
}
public int g()
{
return data;
}
}
class B
{
public A obj1 = new A();
public static void Main()
{
B obj2 = new B();
obj2.obj1.f(20);
Console.WriteLine(obj2.obj1.g());
Console.ReadLine();
}
}
Example: - Using Inheritance
using System;
class A
{
int data;
public void f(int arg)
{
data = arg;
}
public int g()
{
return data;
}
}
class B : A
{
public static void Main()
{
B obj2 = new B();
obj2.f(20);
Console.WriteLine(obj2.g());
Console.ReadLine();
}
}
Example:-
using System;
class BaseDemo
{
public int x = 10;
public BaseDemo()
{
Console.WriteLine("Inheritence BaseDemo class");
}
}
class DerivedDemo:BaseDemo
{
public DerivedDemo()
{
Console.WriteLine("Inheritence DerivedDemo class");
Console.WriteLine("The value of x="+x);
}
}
class inheritenceTest
{
public static void Main()
{
DerivedDemo objderived = new DerivedDemo();
Console.ReadLine();
}
}
Types of Inheritance:-
1. Single Inheritance
2. Multilevel Inheritance
1. Single Inheritance:- In this inheritance one is base class and other is derived class. Some time base class is also called parent class and derived class is also known as sub-class.
Syntax:-
class BaseClass(A) //……………..base class or parent class
{
// fields and methods
}
Class DerivedClass(B): BaseClass(A) //...derived or sub-class child class
{
// fields and methods
}
using System;
class BaseClass
{
public int x = 10;
public BaseClass()
{
Console.WriteLine("Inheritence BaseDemo class");
}
public void BaseMethod()
{
int a = 10, b = 20;
int c = a + b;
Console.WriteLine("The Addition="+c);
}
}
class DerivedDemo:BaseClass
{
public DerivedDemo()
{
Console.WriteLine("Inheritence DerivedDemo class");
Console.WriteLine("The value of x="+x);
}
public void DeriedMethod()
{
Console.WriteLine("I Am Deried Method");
}
}
class inheritenceTest
{
public static void Main()
{
DerivedDemo objderived = new DerivedDemo();
objderived.DeriedMethod();
objderived.BaseMethod();
Console.ReadLine();
}
2. Multi-Level Inheritance:-
In this inheritance we get hierarchical relationship between classes. One Baseclass father for Derivedclass and again that Derivedclass is father for next Derivedclass1 and so on. We will understand clear by following figure(2).
Example:-
using santosh = System.Console; //santosh is alias for System.Console
class Baseclass1
{
public int x = 10;
public Baseclass1()
{
santosh.WriteLine("I am from Baseclass1");
}
}
class Derivedclass1 : Baseclass1
{
public string str = "Santosh Kumar Singh";
public Derivedclass1()
{
santosh.WriteLine("I am from Derivedclass1");
santosh.WriteLine("The value of x=" + x);
}
}
class Derviedclass2:Derivedclass1
{
public Derviedclass2()
{
santosh.WriteLine("I am from Deriedclass2");
santosh.WriteLine("The value str="+str);
}
}
class Test
{
static void Main()
{
Derviedclass2 obj = new Derviedclass2();
santosh.ReadLine();
}
}