Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Introduction to inheritance, polymorphism in C#

Rate me:
Please Sign up or sign in to vote.
4.64/5 (136 votes)
9 Oct 2001CPOL4 min read 998.3K   4.7K   179  
An elementary introduction to inheritance, polymorphism in C# using simple code snippets
using System;
	
class Primary
{
	[STAThread]
	static void Main(string[] args)
	{		
		Animal a1 = new Animal();
		a1.Talk();
		a1.Sing();
		a1.Greet();

		Console.WriteLine();

		Animal a2 = new Dog();
		a2.Talk();
		a2.Sing();
		a2.Greet();

		Console.WriteLine();

		Dog d1 = new Dog();
		d1.Talk();
		d1.Sing();
		d1.Greet();

		Console.WriteLine();

		Color c1 = new Color();
		c1.Fill();
		c1.Fill("red");

		Console.WriteLine();

		Green g1 = new Green();
		g1.Fill();
		g1.Fill("violet");

		Console.WriteLine();

		MicrosoftSoftware m1 = new MicrosoftSoftware();
		//MicrosoftSoftware m2 = new MicrosoftSoftware(300); //won't compile

		Console.WriteLine();

		DundasSoftware du1 = new DundasSoftware(50);

		Console.WriteLine();

		DundasSoftware du2 = new DundasSoftware("test",75);
		
	}
}

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 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 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 DundasSoftware : Software
{
	public DundasSoftware(int y) : base(y)
	{
		Console.WriteLine(m_x);
	}
	public DundasSoftware(string s, int f) : this(f)
	{
		Console.WriteLine(s);
	}
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions