0% found this document useful (0 votes)
38 views6 pages

C# Inheritance and Method Override Examples

The document contains multiple examples of class inheritance in C#, showcasing single-level, multi-level, sealed, override, and abstract classes. Each section includes a 'Tester' class with a 'Main' method that demonstrates the functionality of the defined classes. The examples illustrate how methods can be overridden and how inheritance works in object-oriented programming.

Uploaded by

Gowrav K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

C# Inheritance and Method Override Examples

The document contains multiple examples of class inheritance in C#, showcasing single-level, multi-level, sealed, override, and abstract classes. Each section includes a 'Tester' class with a 'Main' method that demonstrates the functionality of the defined classes. The examples illustrate how methods can be overridden and how inheritance works in object-oriented programming.

Uploaded by

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

Single Level

class Parent
{
public string name = "Rama";

public void show()


{
[Link]("I am in Parent class " + name);
}
}
class Child : Parent
{
public string name1 = "ganga";

public void display()


{
[Link]("Im in Child Class " + name1);
}
}

class Tester
{
public static void Main(string[] args)
{
Child c = new Child();

[Link]([Link]);
[Link](c.name1);

[Link]();
[Link]();
}
}
Multi Level
class A
{
public string name = "Rama";

public void show()


{
[Link]("Im in Parent class " + name);
}
}
class B : A
{
public string name1 = "ganga";

public void display()


{
[Link]("Im in CHild Class " + name1);
}
}

class C : B
{
public string name2 = "Mixture";

public void display2()


{
[Link]("Im in CHild1 " + name2);
}
}
class Tester
{
public static void Main(string[] args)
{
C c = new C();

[Link]([Link]);
[Link](c.name1);
[Link](c.name2);

[Link]();
[Link]();
c.display2();
}
}
Sealed:
public class A
{
public virtual void warmup()
{
[Link]("Hi");
}
}

public class Play:A


{
public sealed override void warmup()
{
[Link]("Run Jump etc");
}

//public abstract void feilding();


}

class Cricket : A
{
public override void warmup()
{
[Link](" Cricket Run Jump etc");
}
}

class Ruby : Cricket


{
public override void warmup()
{
[Link]("Ruby Rum Jump etc");
}
}
using System;
class Tester
{

public static void Main()


{

Play p = new Play();


[Link]();

Cricket c=new Cricket();


[Link]();

Ruby r = new Ruby();


[Link]();

}
}

Override:
public class A
{
public virtual void warmup()
{
[Link]("Hi");
}
}

public class Play:A


{
public override void warmup()
{
[Link]("Run Jump etc");
}

//public abstract void feilding();


}
class Cricket : A
{
public override void warmup()
{
[Link](" Cricket Run Jump etc");
}
}

class Ruby : Cricket


{
public override void warmup()
{
[Link]("Ruby Rum Jump etc");
}
}
using System;
class Tester
{

public static void Main()


{

Play p = new Play();


[Link]();

Cricket c=new Cricket();


[Link]();

Ruby r = new Ruby();


[Link]();

}
}

Abstract:
abstract public class A
{
public abstract void warmup()

public class Play:A


{
public override void warmup()
{
[Link]("Run Jump etc");
}

//public abstract void feilding();


}
using System;
class Tester
{

public static void Main()


{

Play p = new Play();


[Link]();

}
}

You might also like