New Post has been published on Program Solution
New Post has been published on 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β);
#Inherit, #InheritC, #InheritCCode, #InheritDefination








