Make a console C# project and use this code.
Enjoy…
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication_test
{
class testClassRules //test polymorphism, abstract classes, interfaces
{
static void Main(string[] args)
{
//***************************************************************
********************Polymorphism test
/*car c = new car();
benz b = new benz();
porche p = new porche();
Console.WriteLine("This is a car");
c.describeMe();
Console.WriteLine("---------------");
Console.WriteLine("This is a car but a benz");
b.describeMe();
Console.WriteLine("...............");
Console.WriteLine("This is a car");
c.describeMe();
Console.WriteLine("---------------");
Console.WriteLine("This is a car but a porche");
p.describeMe();
Console.WriteLine("...............");
Console.WriteLine("This is a car but a benz");
b.describeMe();
Console.WriteLine("...............");
Console.WriteLine("This is a car");
c.describeMe();
Console.WriteLine("...............");*/
/*
//test new
mycar[] cars = new mycar[3];
cars[0] = new mycar();
cars[1] = new benz();
cars[2] = new porche();
foreach (mycar c in cars)
{
c.normalTest();
//c.describeMe();
c.virNewTest();
//c.newTest();
Console.WriteLine("\n-------------------------\n");
}*/
//Console.WriteLine("-------------------------\n");
//DerivedClass B = new DerivedClass();
//B.DoWork(); // Calls the new method.
///*BaseClass A = (BaseClass)B;
//A.DoWork(); // Also calls the new method.*/
//BaseClass A = new BaseClass();
//A.DoWork();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//mycar c = new mycar();
//benz b = new benz();
//porche p = new porche();
//c.newTest();
//b.newTest();
//p.newTest();
//c.describeMe();
//b.describeMe();
//p.describeMe();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//mycar[] cars = new mycar[3];
//cars[0] = new mycar();
//cars[1] = new benz();
//cars[2] = new porche();
//foreach (mycar mc in cars)
//{
// //mc.normalTest();
// //mc.describeMe();
// //mc.virNewTest();
// //((benz)mc).newTest();
// mc.newTest();
//}
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//((mycar)cars[0]).newTest();
//((benz)cars[1]).newTest();
//((porche)cars[2]).newTest();
//((mycar)cars[0]).describeMe();
//((benz)cars[1]).describeMe();
//((porche)cars[2]).describeMe();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//Car c1 = new Car();
//ConvertibleCar cc = new ConvertibleCar();
//Minivan mv = new Minivan();
//c1.DescribeCar();
//cc.DescribeCar();
//mv.DescribeCar();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//Car[] myC = new Car[3];
//myC[0] = new Car();
//myC[1] = new ConvertibleCar();
//myC[2] = new Minivan();
//foreach( Car cm in myC )
//{
// cm.DescribeCar();
//}
//***************************************************************
********************Abstract test
//abstractBaseClass a = new abstractBaseClass("test"); //not
possible, cannot instantiate an abstract class
//derivedAbstract d = new derivedAbstract("test");
//d.i = 4;
//System.Console.WriteLine("The value of i is {0}", d.i);
//System.Console.WriteLine(d.getName());
//d.meth();
//derivedAbstract te = new derivedAbstract("this is just a test");
//te.testField = "this is values through the field itself";
//System.Console.WriteLine(te.testField);
//te.testProperty = "\nthis is values through its property";
//System.Console.WriteLine(te.testProperty);
//***************************************************************
********************Interface test
//ICar c = new ICar(); //Cannot instantiate an interface
ZZR z1 = new ZZR("My first ZZR", "EDFV676GHG0");
ZZR z2 = new ZZR("My second ZZR", "EDFV676GHG0", 890, 7);
//System.Console.WriteLine("Specification of z1");
System.Console.WriteLine("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", z1.enginNo, z1.Volume,
z1.cylinderNo, z1.vehicleName);
System.Console.WriteLine("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", z2.enginNo, z2.Volume,
z2.cylinderNo, z2.vehicleName);
//System.Console.WriteLine(z1.EnginNo);
Boxter b = new Boxter("My Boxter", "EDFV676GH980", 2700, 6);
System.Console.WriteLine("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", b.enginNo, b.Volume,
b.cylinderNo, b.vehicleName);
Strange1 s1 = new Strange1("\n\n\nStrange1", "BGHVX^&HSGY", 9876,
12);
System.Console.WriteLine("Property CylinderNo", s1.CylinderNo);
//System.Console.WriteLine("Method CylNo",
Convert.ToString((double)(s1.cylNo)));
System.Console.WriteLine("Method CylNo", s1.EnginNo);
Strange2 s2 = new Strange2();
s2.volume = 4;
s2.EnginNo = "None of the interfaces implementation";
System.Console.WriteLine("\n\nProperty Volume:{0}", s2.volume);
System.Console.WriteLine("Method CylNo[of ICar interface]:{0}",
((ICar)s2).cylNo());
System.Console.WriteLine("No interfaces implementation:{0}",
s2.EnginNo); //this is not possible because each interfaces has their won
implementation of
//Engine
No and class Strange2 doesnt have a defnition foe EngineNo itself
//unless
we define a seperate EngineNo property for class itself
((ICar)s2).EnginNo = "CAR_HSHSJH877JH";
((IMotorBike)s2).EnginNo = "MOTORBIKR_HGBGF676G7";
System.Console.WriteLine("\n\nEngine No(of ICar interface):{0}",
((ICar)s2).EnginNo);
System.Console.WriteLine("Engine No(of IMotorBike
interface):{0}", ((IMotorBike)s2).EnginNo);
//or
ICar cs2 = (ICar)s2;
//cs2.EnginNo = "CAR_HSHSJH877JH";
System.Console.WriteLine("\n\nEngine No(of ICar interface):{0}",
cs2.EnginNo);
IMotorBike ms2 = (IMotorBike)s2;
//ms2.EnginNo = "MOTORBIKR_HGBGF676G7";
System.Console.WriteLine("\n\nEngine No(of IMotorBike
interface):{0}", ms2.EnginNo);
//************************************************ Exit Part
*******************************************//
System.Console.Write("\n\n\npress any key to exit...");
System.Console.ReadKey();
}
}
}
//***************************************************************Polymorphism
examples
//***************************************************************************
*********
public class mycar
{
public virtual void describeMe()
{
Console.WriteLine("I am a car");
}
public void newTest()
{
Console.WriteLine("I am in new:car");
}
public virtual void virNewTest()
{
Console.WriteLine("I am in new virtual:car");
}
public void normalTest()
{
Console.WriteLine("I am in normal test:car");
}
}
public class benz : mycar
{
public override void describeMe()
{
//base.describeMe();
Console.WriteLine("And I am a benz");
}
public new void newTest()
{
Console.WriteLine("I am in new:benz");
}
public new void virNewTest()
{
Console.WriteLine("I am in new virtual:benz");
}
public new void normalTest()
{
Console.WriteLine("I am in normal test:benz");
}
}
public class porche : mycar
{
public override void describeMe()
{
Console.WriteLine("i am a porche");
}
public new void newTest()
{
Console.WriteLine("I am in new:porche");
}
public new void virNewTest()
{
Console.WriteLine("I am in new virtual:porche");
}
public new void normalTest()
{
Console.WriteLine("I am in normal test:porche");
}
}
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine("Just a car.");
}
}
class ConvertibleCar : Car
{
public new virtual void DescribeCar()
{
System.Console.WriteLine("a convertable car.");
}
}
class Minivan : Car
{
public override void DescribeCar()
{
System.Console.WriteLine("a minivan car.");
}
}
public class BaseClass
{
public virtual void DoWork()
{
System.Console.WriteLine("doWork in 'BaseClass'");
}
public virtual int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public override void DoWork()
{
System.Console.WriteLine("doWork in 'DerivedClass'");
}
public override int WorkProperty
{
get { return 0; }
}
}
//***************************************************************Abstract
example
//***************************************************************************
****
public abstract class abstractBaseClass
{
private string name;
public int i; //(*)
public string testField;
public int I //this one should be declared as lower case before at (*)
and should be capitall i if it is in lower case then you will have stack
exception overflow errors
{
get { return i; }
set { i = value; }
}
public string testProperty //a property has same access level and type
of the field that it has access to but it should have a different name
{
get { return (this.testField); }
set { this.testField = value; }
}
public abstractBaseClass(string str)
{
this.name = str;
}
public string getName()
{
return this.name;
}
public virtual void test()
{
System.Console.WriteLine("i am in test:abstractBaseClass");
}
//public abstract void meth() { } //because it is abstract so it cannot
have a body
//so it should be declared as below
public abstract void meth();
//public abstract virtual void abstVirt(); //an abstract method cannot
be virtual because it cannot have a body anyway
//so it should be declared either as virtual or abstract
public virtual void testVirt(){}
}
public class derivedAbstract : abstractBaseClass
{
//public int j;
//public int i
//{
// get { return i; }
// set { j = value; }
//}
public derivedAbstract(string str) : base(str)
{
//:base(str);
}
public override void test()
{
System.Console.WriteLine("i am in test:abstractBaseClass");
}
//public void meth() //because this is an abstract method in base class
so we should explicitle mention override key word
//so it should be written as below
public override void meth()
//{
// throw new Exception("The method or operation is not implemented.");
//}
{
System.Console.WriteLine("this is the implementation of an abstract
method");
}
}
//***************************************************************Interface
example
//***************************************************************************
*****
interface IVehicle
{
//public string vId; //error interfaces cannot contain fields
//string vId //interfacase cannot have properties this way becauser
we have the definition
//{
// get { } //we shouldnt have those brackets so only get;
// set { }
//}
string vehicleName
{
get;
set;
}
interface ICar : IVehicle
{
//public string engineNo //interfaces cannot contain modifier public
string EnginNo
{
get;
set;
}
int Volume
{
get;
set;
}
int cylNo();
}
interface IMotorBike : IVehicle
{
string EnginNo
{
get;
set;
}
int Volume
{
get;
set;
}
int cylNo();
}
public class ZZR : IMotorBike
{
public string vName;
public string enginNo;
public int volume;
public int cylinderNo;
public ZZR(string name, string eNo, int vol, int cyl)
{
//this.engineNo(eNo); //this is wrong because we have treated a
property like a method
this.enginNo = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
this.cylinderNo = cyl;
//this.volume(vol); //this is wrong because we have treated a
property like a method
this.volume = vol;
this.vName = name;
}
public ZZR(string name, string eNo)
{
this.enginNo = eNo;
this.cylinderNo = 6;
this.volume = 750;
this.vName = name;
}
public string vehicleName
{
get { return (this.vName); }
set { this.vName = value; }
}
public string EnginNo
{
get { return(this.enginNo); }
set { this.enginNo = value; }
}
public int Volume
{
get { return (this.volume); }
set {this.volume = value;}
}
public int CylinderNo
{
get { return (this.cylinderNo); }
set { this.cylinderNo = value; }
}
public int cylNo()
{
return(this.cylinderNo);
}
}
public class Boxter : ICar
{
public string vName;
public string enginNo;
public int volume;
public int cylinderNo;
public Boxter(string name, string eNo, int vol, int cyl)
{
//this.engineNo(eNo); //this is wrong because we have treated a
property like a method
this.enginNo = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
this.cylinderNo = cyl;
//this.volume(vol); //this is wrong because we have treated a
property like a method
this.volume = vol;
this.vName = name;
}
public string vehicleName
{
get { return (this.vName); }
set { this.vName = value; }
}
public string EnginNo
{
get { return (this.enginNo); }
set { this.enginNo = value; }
}
public int Volume
{
get { return (this.volume); }
set {this.volume = value;}
}
public int CylinderNo
{
get { return (this.cylinderNo); }
set { this.cylinderNo = value; }
}
public int cylNo()
{
return (this.cylinderNo);
}
}
public class Strange1 : ICar, IMotorBike
{
public string vName;
public string enginNo;
public int volume;
public int cylinderNo;
public Strange1(string name, string eNo, int vol, int cyl)
{
//this.engineNo(eNo); //this is wrong because we have treated a
property like a method
this.enginNo = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
this.cylinderNo = cyl;
//this.volume(vol); //this is wrong because we have treated a
property like a method
this.volume = vol;
this.vName = name;
}
public string vehicleName
{
get { return (this.vName); }
set { this.vName = value; }
}
public string EnginNo
{
get { return (this.enginNo); }
set { this.enginNo = value; }
}
public int Volume
{
get { return (this.volume); }
set {this.volume = value;}
}
public int CylinderNo
{
get { return (this.cylinderNo); }
set { this.cylinderNo = value; }
}
public int cylNo()
{
return (this.cylinderNo);
}
}
public class Strange2 : ICar, IMotorBike
{
public string vName;
public string enginNo;
public int volume;
public int cylinderNo;
public string vehicleName
{
get { return (this.vName); }
set { this.vName = value; }
}
//public string ICar.EnginNo //when we specify explicitly which part of
which interface we are implementing we shouldnt sepecify the modifier
string ICar.EnginNo
{
get { return (this.enginNo); }
set { this.enginNo = value; }
}
public string EnginNo
{
get { return (this.enginNo); }
set { this.enginNo = value; }
}
//public int ICar.Volume
int ICar.Volume
{
get { return (this.volume); }
set {this.volume = value;}
}
//public string IMotorBike.EnginNo
string IMotorBike.EnginNo
{
get { return (this.enginNo + "XXX"); }
set { this.enginNo = value ; }
}
//public int IMotorBike.Volume
int IMotorBike.Volume
{
get { return (this.volume); }
set { this.volume = value - 100; }
}
public int CylinderNo
{
get { return (this.cylinderNo); }
set { this.cylinderNo = value; }
}
//when we explicitle implement one part of one of the interfaces that
this class inherits, we should implements the same part of the other
interfaces
//this is true in this case that both interfaces have a method with the
same name
//public int ICar.cylNo()
int ICar.cylNo()
{
return (this.cylinderNo);
}
//public int IMotorBike.cylNo()
int IMotorBike.cylNo()
{
return (this.cylinderNo);
}
}