Make a console C# project and use this code.
Enjoy…
using System;
using [Link];
using [Link];
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();
[Link]("This is a car");
[Link]();
[Link]("---------------");
[Link]("This is a car but a benz");
[Link]();
[Link]("...............");
[Link]("This is a car");
[Link]();
[Link]("---------------");
[Link]("This is a car but a porche");
[Link]();
[Link]("...............");
[Link]("This is a car but a benz");
[Link]();
[Link]("...............");
[Link]("This is a car");
[Link]();
[Link]("...............");*/
/*
//test new
mycar[] cars = new mycar[3];
cars[0] = new mycar();
cars[1] = new benz();
cars[2] = new porche();
foreach (mycar c in cars)
{
[Link]();
//[Link]();
[Link]();
//[Link]();
[Link]("\n-------------------------\n");
}*/
//[Link]("-------------------------\n");
//DerivedClass B = new DerivedClass();
//[Link](); // Calls the new method.
///*BaseClass A = (BaseClass)B;
//[Link](); // Also calls the new method.*/
//BaseClass A = new BaseClass();
//[Link]();
//[Link]("\n-------------------------");
//[Link]("-------------------------\n");
//mycar c = new mycar();
//benz b = new benz();
//porche p = new porche();
//[Link]();
//[Link]();
//[Link]();
//[Link]();
//[Link]();
//[Link]();
//[Link]("\n-------------------------");
//[Link]("-------------------------\n");
//mycar[] cars = new mycar[3];
//cars[0] = new mycar();
//cars[1] = new benz();
//cars[2] = new porche();
//foreach (mycar mc in cars)
//{
// //[Link]();
// //[Link]();
// //[Link]();
// //((benz)mc).newTest();
// [Link]();
//}
//[Link]("\n-------------------------");
//[Link]("-------------------------\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();
//[Link]("\n-------------------------");
//[Link]("-------------------------\n");
//Car c1 = new Car();
//ConvertibleCar cc = new ConvertibleCar();
//Minivan mv = new Minivan();
//[Link]();
//[Link]();
//[Link]();
//[Link]("\n-------------------------");
//[Link]("-------------------------\n");
//Car[] myC = new Car[3];
//myC[0] = new Car();
//myC[1] = new ConvertibleCar();
//myC[2] = new Minivan();
//foreach( Car cm in myC )
//{
// [Link]();
//}
//***************************************************************
********************Abstract test
//abstractBaseClass a = new abstractBaseClass("test"); //not
possible, cannot instantiate an abstract class
//derivedAbstract d = new derivedAbstract("test");
//d.i = 4;
//[Link]("The value of i is {0}", d.i);
//[Link]([Link]());
//[Link]();
//derivedAbstract te = new derivedAbstract("this is just a test");
//[Link] = "this is values through the field itself";
//[Link]([Link]);
//[Link] = "\nthis is values through its property";
//[Link]([Link]);
//***************************************************************
********************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);
//[Link]("Specification of z1");
[Link]("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", [Link], [Link],
[Link], [Link]);
[Link]("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", [Link], [Link],
[Link], [Link]);
//[Link]([Link]);
Boxter b = new Boxter("My Boxter", "EDFV676GH980", 2700, 6);
[Link]("\nSpecification of '{3}'\nEngin
No:{0}\nEngine Volume:{1}\nNumber of Cylinders:{2}", [Link], [Link],
[Link], [Link]);
Strange1 s1 = new Strange1("\n\n\nStrange1", "BGHVX^&HSGY", 9876,
12);
[Link]("Property CylinderNo", [Link]);
//[Link]("Method CylNo",
[Link]((double)([Link])));
[Link]("Method CylNo", [Link]);
Strange2 s2 = new Strange2();
[Link] = 4;
[Link] = "None of the interfaces implementation";
[Link]("\n\nProperty Volume:{0}", [Link]);
[Link]("Method CylNo[of ICar interface]:{0}",
((ICar)s2).cylNo());
[Link]("No interfaces implementation:{0}",
[Link]); //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";
[Link]("\n\nEngine No(of ICar interface):{0}",
((ICar)s2).EnginNo);
[Link]("Engine No(of IMotorBike
interface):{0}", ((IMotorBike)s2).EnginNo);
//or
ICar cs2 = (ICar)s2;
//[Link] = "CAR_HSHSJH877JH";
[Link]("\n\nEngine No(of ICar interface):{0}",
[Link]);
IMotorBike ms2 = (IMotorBike)s2;
//[Link] = "MOTORBIKR_HGBGF676G7";
[Link]("\n\nEngine No(of IMotorBike
interface):{0}", [Link]);
//************************************************ Exit Part
*******************************************//
[Link]("\n\n\npress any key to exit...");
[Link]();
}
}
}
//***************************************************************Polymorphism
examples
//***************************************************************************
*********
public class mycar
{
public virtual void describeMe()
{
[Link]("I am a car");
}
public void newTest()
{
[Link]("I am in new:car");
}
public virtual void virNewTest()
{
[Link]("I am in new virtual:car");
}
public void normalTest()
{
[Link]("I am in normal test:car");
}
}
public class benz : mycar
{
public override void describeMe()
{
//[Link]();
[Link]("And I am a benz");
}
public new void newTest()
{
[Link]("I am in new:benz");
}
public new void virNewTest()
{
[Link]("I am in new virtual:benz");
}
public new void normalTest()
{
[Link]("I am in normal test:benz");
}
}
public class porche : mycar
{
public override void describeMe()
{
[Link]("i am a porche");
}
public new void newTest()
{
[Link]("I am in new:porche");
}
public new void virNewTest()
{
[Link]("I am in new virtual:porche");
}
public new void normalTest()
{
[Link]("I am in normal test:porche");
}
}
class Car
{
public virtual void DescribeCar()
{
[Link]("Just a car.");
}
}
class ConvertibleCar : Car
{
public new virtual void DescribeCar()
{
[Link]("a convertable car.");
}
}
class Minivan : Car
{
public override void DescribeCar()
{
[Link]("a minivan car.");
}
}
public class BaseClass
{
public virtual void DoWork()
{
[Link]("doWork in 'BaseClass'");
}
public virtual int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public override void DoWork()
{
[Link]("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 ([Link]); }
set { [Link] = value; }
}
public abstractBaseClass(string str)
{
[Link] = str;
}
public string getName()
{
return [Link];
}
public virtual void test()
{
[Link]("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()
{
[Link]("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.");
//}
{
[Link]("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)
{
//[Link](eNo); //this is wrong because we have treated a
property like a method
[Link] = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
[Link] = cyl;
//[Link](vol); //this is wrong because we have treated a
property like a method
[Link] = vol;
[Link] = name;
}
public ZZR(string name, string eNo)
{
[Link] = eNo;
[Link] = 6;
[Link] = 750;
[Link] = name;
}
public string vehicleName
{
get { return ([Link]); }
set { [Link] = value; }
}
public string EnginNo
{
get { return([Link]); }
set { [Link] = value; }
}
public int Volume
{
get { return ([Link]); }
set {[Link] = value;}
}
public int CylinderNo
{
get { return ([Link]); }
set { [Link] = value; }
}
public int cylNo()
{
return([Link]);
}
}
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)
{
//[Link](eNo); //this is wrong because we have treated a
property like a method
[Link] = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
[Link] = cyl;
//[Link](vol); //this is wrong because we have treated a
property like a method
[Link] = vol;
[Link] = name;
}
public string vehicleName
{
get { return ([Link]); }
set { [Link] = value; }
}
public string EnginNo
{
get { return ([Link]); }
set { [Link] = value; }
}
public int Volume
{
get { return ([Link]); }
set {[Link] = value;}
}
public int CylinderNo
{
get { return ([Link]); }
set { [Link] = value; }
}
public int cylNo()
{
return ([Link]);
}
}
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)
{
//[Link](eNo); //this is wrong because we have treated a
property like a method
[Link] = eNo;
//cylinderNo(cyl); //this is wrong because we have treated a
property like a method
[Link] = cyl;
//[Link](vol); //this is wrong because we have treated a
property like a method
[Link] = vol;
[Link] = name;
}
public string vehicleName
{
get { return ([Link]); }
set { [Link] = value; }
}
public string EnginNo
{
get { return ([Link]); }
set { [Link] = value; }
}
public int Volume
{
get { return ([Link]); }
set {[Link] = value;}
}
public int CylinderNo
{
get { return ([Link]); }
set { [Link] = value; }
}
public int cylNo()
{
return ([Link]);
}
}
public class Strange2 : ICar, IMotorBike
{
public string vName;
public string enginNo;
public int volume;
public int cylinderNo;
public string vehicleName
{
get { return ([Link]); }
set { [Link] = value; }
}
//public string [Link] //when we specify explicitly which part of
which interface we are implementing we shouldnt sepecify the modifier
string [Link]
{
get { return ([Link]); }
set { [Link] = value; }
}
public string EnginNo
{
get { return ([Link]); }
set { [Link] = value; }
}
//public int [Link]
int [Link]
{
get { return ([Link]); }
set {[Link] = value;}
}
//public string [Link]
string [Link]
{
get { return ([Link] + "XXX"); }
set { [Link] = value ; }
}
//public int [Link]
int [Link]
{
get { return ([Link]); }
set { [Link] = value - 100; }
}
public int CylinderNo
{
get { return ([Link]); }
set { [Link] = 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 [Link]()
int [Link]()
{
return ([Link]);
}
//public int [Link]()
int [Link]()
{
return ([Link]);
}
}