How to Inherit in C# with Classes
https://www.saqibsomal.com/c-sharp/how-to-inherit-in-c-with-classes/16
How to Inherit in C# with Classes
Inheritance, in CĀ Sharp, is the ability to create a class that inherits attributes and behaviors from an existing class. The newly created class is theĀ derivedĀ or child class and the existing class is the base (or parent) class. Inheritance is one of the key features of object-oriented programming.
Example 1Ā
using System; class Animal public Animal() Console.WriteLine(āAnimal constructorā); public void Greet() Console.WriteLine(āAnimal says Helloā); public void Talk() Console.WriteLine(āAnimal talkā); public virtual void Sing() Console.WriteLine(āAnimal songā);
class Dog : Animal public Dog() Console.WriteLine(āDog constructorā); public new void Talk() Console.WriteLine(āDog talkā); public override void Sing() Console.WriteLine(āDog songā);
class test static void Main() Animal a1 = new Animal(); a1.Talk(); a1.Sing(); a1.Greet();
Ā Example 2
Ā using System; class Software public Software() m_x = 100; public Software(int y) m_x = y; protected int m_x;
class MicrosoftSoftware : Software public MicrosoftSoftware() Console.WriteLine(m_x);
class test static void Main() MicrosoftSoftware m1 = new MicrosoftSoftware();
Ā Example 3Ā
using System;
class Color public virtual void Fill() Console.WriteLine(āFill me up with colorā); public void Fill(string s) Console.WriteLine(āFill me up with 0ā,s); class Green : Color public override void Fill() Console.WriteLine(āFill me up with greenā);
class colortry
static void Main() Green g1 = new Green(); g1.Fill(); g1.Fill(āvioletā);

















