Inheritance:
Inheritance is a process of deriving the new class from
already existing class
C# is a complete object
oriented programming language. Inheritance is one of the primary concepts of
object-oriented programming. It allows you to reuse existing code. Through
effective use of inheritance, you can save lot of time in your programming and
also reduce errors, which in turn will increase the quality of work and
productivity. A simple example to understand inheritance in C#.
Using
System;
Public class BaseClass
{
Public BaseClass ()
{
Console.WriteLine ("Base Class Constructor executed");
}
Public void
Write ()
{
Console.WriteLine ("Write method in Base Class executed");
}
}
Public class ChildClass: BaseClass
{
Public ChildClass ()
{
Console.WriteLine("Child Class Constructor executed");
}
Public static
void Main ()
{
ChildClass CC = new ChildClass ();
CC.Write ();
}
}
In
the Main () method in ChildClass we create an instance of childclass. Then we
call the write () method. If you observe the ChildClass does not have a write()
method in it. This write () method has been inherited from the parent
BaseClass.
The
output of the above program is
Output:
Output:
Base Class Constructor executed
Child Class Constructor executed
Write method in Base Class executed
Child Class Constructor executed
Write method in Base Class executed
this
output proves that when we create an instance of a child class, the base class
constructor will automatically be called before the child class constructor. So
in general Base classes are automatically instantiated before derived classes.
In
C# the syntax for specifying BaseClass and ChildClass relationship is shown
below. The base class is specified by adding a colon, ":", after the
derived class identifier and then specifying the base class name.
Syntax: class ChildClassName:
BaseClass
{
//Body
}
{
//Body
}
C#
supports single class inheritance only. What this means is, your class can
inherit from only one base class at a time. In the code snippet below, class C
is trying to inherit from Class A and B at the same time. This is not allowed
in C#. This will lead to a compile time
error: Class 'C' cannot have
multiple base classes: 'A' and 'B'.
public class
A
{
}
public class
B
{
}
public class
C : A, B
{
}
In
C++ Multi-Level inheritance is possible. Code snippet below demonstrates multi-level
inheritance. Class B is derived from Class A. Class C is derived from Class B.
So class C, will have access to all members present in both Class A and Class
B. As a result of multi-level inheritance Class has access to
A_Method(),B_Method() and C_Method().
Note: Classes can inherit from multiple interfaces at the same time. Interview Question: How can you implement multiple inheritance in C#? Ans : Using Interfaces. We will talk about interfaces in our later article.
Note: Classes can inherit from multiple interfaces at the same time. Interview Question: How can you implement multiple inheritance in C#? Ans : Using Interfaces. We will talk about interfaces in our later article.
Using System;
Public class
A
{
Public void A_Method ()
{
Console.WriteLine ("Class A Method Called");
}
}
Public class
B: A
{
Public void B_Method ()
{
Console.WriteLine ("Class A Method Called");
}
}
Public class
C: B
{
Public void C_Method ()
{
Console.WriteLine ("Class A Method Called");
}
Public static void Main ()
{
C C1 = new
C ();
C1.A_Method ();
C1.B_Method ();
C1.C_Method ();
}
}
When
you derive a class from a base class, the derived class will inherit all
members of the base class except constructors. In the code snippet below class
B will inherit both M1 and M2 from Class A, but you cannot access M2 because of
the private access modifier. Class members declared with a private access
modifier can be accessed only with in the class. We will talk about access
modifiers in our later article.
No comments:
Post a Comment