Skip to content

Instantly share code, notes, and snippets.

@ThanosRestas
Last active September 30, 2022 09:39
Abstract,Interfaces,Virtual usage in C# Demo
using System;
public interface IBasicAbilities
{
void MotherLanguage ();
}
public abstract class Entity
{
protected string name;
protected bool isAlive;
protected int numberOfLegs;
public void IsAlive ()
{
if (isAlive)
{
Console.WriteLine (this.GetType () + " : " + name + " is Alive.");
}
else
{
Console.WriteLine (this.GetType () + " : " + name + " is not Alive.");
}
}
public void HowManyLegs ()
{
Console.WriteLine (this.GetType () + " : " + name + " has : " + numberOfLegs + " legs.");
}
public void GetName ()
{
Console.WriteLine (this.GetType () + " is named : " + name);
}
public virtual void isHungry ()
{
Console.WriteLine (name + " needs to be fed !");
}
public abstract void isHappy ();
}
public class Human : Entity, IBasicAbilities
{
string language;
public Human (string userName, bool userIsAlive, int userNumberOfLegs, string userLanguage)
{
name = userName;
isAlive = userIsAlive;
numberOfLegs = userNumberOfLegs;
language = userLanguage;
}
public override void isHappy ()
{
Console.WriteLine (this.GetType () + " named " + name + " is happy !");
}
public void MotherLanguage ()
{
Console.WriteLine (name + " speaks " + language);
}
}
public class Furniture : Entity
{
public Furniture (string userName, bool userIsAlive, int userNumberOfLegs)
{
name = userName;
isAlive = userIsAlive;
numberOfLegs = userNumberOfLegs;
}
public override void isHappy ()
{
Console.WriteLine ("A " + this.GetType ().Name.ToLower () + " like a " + name.ToLower () + " can't be happy...or sad.");
}
public override void isHungry ()
{
Console.WriteLine ("A " + this.GetType ().Name.ToLower () + " doesn't get hungry.");
}
}
public class Plant : Entity
{
public Plant (string userName, bool userIsAlive, int userNumberOfLegs)
{
name = userName;
isAlive = userIsAlive;
numberOfLegs = userNumberOfLegs;
}
public override void isHappy ()
{
Console.WriteLine ("No cause you forgot to water the " + name);
}
}
public class Test
{
public static void Main ()
{
Human subject1 = new Human ("Brodude", true, 2, "english");
subject1.GetName ();
subject1.IsAlive ();
subject1.HowManyLegs ();
subject1.isHappy ();
subject1.isHungry ();
subject1.MotherLanguage ();
Furniture subject2 = new Furniture ("Chair", false, 4);
subject2.GetName ();
subject2.IsAlive ();
subject2.HowManyLegs ();
subject2.isHappy ();
subject2.isHungry ();
Plant subject3 = new Plant ("Venus flytrap", true, 0);
subject3.GetName ();
subject3.IsAlive ();
subject3.HowManyLegs ();
subject3.isHappy ();
subject3.isHungry ();
//IBasicAbilities subject4 = new Human("Test", false, 2, "german");
//subject4.MotherLanguage();
//Human subject4 = new Human("Test", false, 2, "german");
//subject4.GetName();
//subject4.IsAlive();
//subject4.HowManyLegs();
//subject4.isHappy();
//subject4.isHungry();
//subject4.MotherLanguage();
}
}
/*--- Output ---
Human is named : Brodude
Human : Brodude is Alive.
Human : Brodude has : 2 legs.
Human named Brodude is happy !
Brodude needs to be fed !
Brodude speaks english
Furniture is named : Chair
Furniture : Chair is not Alive.
Furniture : Chair has : 4 legs.
A furniture like a chair can't be happy...or sad.
A furniture doesn't get hungry.
Plant is named : Venus flytrap
Plant : Venus flytrap is Alive.
Plant : Venus flytrap has : 0 legs.
No cause you forgot to water the Venus flytrap
Venus flytrap needs to be fed !
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment