0% found this document useful (0 votes)
18 views13 pages

C# Inheritance Explained: Types & Syntax

inheritance in c#
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

C# Inheritance Explained: Types & Syntax

inheritance in c#
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

▪ In C#, inheritance is a process in which one object acquires all

the properties and behaviors of its parent object automatically.

▪ In such way, we can reuse, extend or modify the attributes and


behaviors which is defined in other class.

▪ Derived Class (child) - the class that inherits from another


class
▪ Base Class (parent) - the class being inherited from

▪ The derived class is the specialized class for the base class.

▪ Advantage of C# Inheritance
▪ Code reusability: Now you can reuse the members of your parent
class. So, there is no need to define the member again. So less code
is required in the class.
Syntax

[<modifiers>] class <child class> : <parent class>

▪ To inherit from a class, use the : symbol.


▪ In the example, the B class (child) inherits the fields and methods from the A class (parent):

Note: In Inheritance, the Child class can consume members of its Parent class as if it is the owner of those
members (except private members of the parent).
using System; class B : A
namespace InheritanceDemo {
{ static void Main()
class A {
{ B obj = new B();
public void Method1() obj.Method1();
{ obj.Method2();
[Link]("Method 1"); [Link]();
} }
public void Method2() }
{ }
[Link]("Method 2");
}
}
When we are working with Inheritance, some rules are required to be followed:
▪ In C#, the parent classes constructor must be accessible to the child class; otherwise, the
inheritance would not be possible. If a constructor is defined implicitly, then it is a public
constructor.
▪ Example:
class A
{
public A()
{
[Link]("Class A Constructor is Called");
}
public void Method1()
{
[Link]("Method 1");
}
▪ In inheritance, the child class can access the parent class members, but the parent
classes can never access any members that are purely defined in the child class.
▪ If you don't want other classes to inherit from a class, use the sealed keyword:
▪ If you try to access a sealed class, C# will generate an error:

sealed class Vehicle


{
...
}

class Car : Vehicle


{
...
}

▪ The error message will be something like this:


'Car': cannot derive from sealed type 'Vehicle'
In C#, there are 4 types of inheritance:
1. Single inheritance: A derived class that inherits from only one base class.
2. Multi-level inheritance: A derived class that inherits from a base class and the
derived class itself becomes the base class for another derived class.
3. Hierarchical inheritance: A base class that serves as a parent class for two or
more derived classes.
4. Multiple inheritance: A derived class that inherits from two or more base
classes.
▪ It is the type of inheritance in which Example

there is one base class and one public class Accountcreditinfo //base class
derived class. {
public string Credit()
{
return "balance is credited";
}
}
public class debitinfo : Accountcreditinfo //derived class
{
public string debit()
{
Credit(); ////derived class
method
return "balance is debited";
}
}
class Animal

Use of ‘protected’ {
protected string sound = "Some sound";

access specifier }

class Dog : Animal


{
▪ The protected access specifier public void Bark()
allows a member of a class to be {
accessible within its own class
and by derived classes (child sound = "Bark"; // Accessing protected member
classes), but not from outside [Link](sound);
these classes. }
▪ The given example demonstrates }
single inheritance. In this type of
inheritance, a class (in this case,
Dog) inherits from only one base class Program
class (Animal). {
static void Main()
{
Dog dog = new Dog();
[Link](); // Output: Bark
}
}
▪ This is the type of inheritance in which there Example
are multiple classes derived from one base class A //base class
class. This type of inheritance is used when
{
there is a requirement of one class feature
that is needed in multiple classes. -------------
}
class B : A
{
------------
class C : A
{
------------
}
}
▪ When one class is derived from Example
another, this type of inheritance is
called multilevel inheritance. public class A
{
-----------------
}
public class B : A
{
----------------
}
public class C : B
{
----------------
}
public interface IA //ineterface 1
▪ C# does not support multiple {
inheritances of classes. To overcome this
problem, we can use interfaces. string setImgs(string a);
}
public interface IB //Interface 2
{
int getAmount(int Amt);
}
public class ICar : IA, IB //implementation
{
public int getAmount(int Amt)
{
return 100;
}
public string setImgs(string a)
{
return "this is the car";
}
}

You might also like