What is an Inheritance in C#?
Hi friends,
I am new to the programming language. And I have started with the C# language. I have gone through the Inheritance, but I was unable to understand clearly. There are lot of doubts regarding the inheritance. So can anyone tell me what is an Inheritance in C#? Please help me soon..!!!:notworthy
Re: What is an Inheritance in C#?
you should have enough knowledge about the Classes and Structs before moving to the Inheritance in C#. Classes and Structs are ‘blue-prints’ or templates from which we create objects. An object can be of the following types – Class or Struct . Also an Inheritance is one of the most important characteristic of OOP. Classes are also called reference types Structs are known as value types Classes.
Re: What is an Inheritance in C#?
C# supports two types of Inheritance mechanisms
- Implementation Inheritance
- Interface Inheritance
Implementation Inheritance - When a class is derived from another class is called as implementation inheritance.
Interface Inheritance - When a type inherits only the signatures of the functions from another type it is interface inheritance.
Re: What is an Inheritance in C#?
I think that providing the syntax of both types of implementation will help you, so I provided that :
Following is an syntax example for using Implementation Inheritance :
Code:
Class derivedClass:baseClass
{
}
derivedClass is derived from baseClass
The following is an example of Interface Inheritance :
Code:
private Class derivedClass:baseClass , InterfaceX , InterfaceY
{
}
derivedClass is now derived from interfaces.
Re: What is an Inheritance in C#?
You can use the following example for knowing about the Inheritance in C# :
Code:
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("This is a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}