What is Inheritance?
Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Inheritance makes it easier to create and maintain an application.
How to implement inheritance
First we have to create parent class with some methods in BaseClass.cs
- using System;
- namespace OopsTutorial
- {
- class BaseClass
- {
- internal BaseClass()
- {
- Console.WriteLine("Baseclass Constructor Executed!!");
- }
- internal void Print()
- {
- Console.WriteLine("BaseClass Print Method Executed");
- }
- }
- class ChildClass : BaseClass
- {
- internal ChildClass()
- {
- Console.WriteLine("ChildClass Constructor Executed!!");
- }
- internal new void Print()
- {
- Console.WriteLine("ChildClass Print Method Executed");
- }
- }
- }
In program.cs
- using System;
- namespace OopsTutorial
- {
- class Program
- {
- static void Main(string[] args)
- {
- BaseClass bc = new BaseClass();
- ChildClass cc = new ChildClass();
- BaseClass bccc = new ChildClass();
- bc.Print();
- cc.Print();
- Console.WriteLine("============");
- bccc.Print();
- Console.ReadKey();
- }
- }
- }
Output :