Polymorphis Abstract Interface SampleCode4CSharp
Polymorphis Abstract Interface SampleCode4CSharp
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
Console.WriteLine("This is a car");
c.describeMe();
Console.WriteLine("---------------");
Console.WriteLine("This is a car");
c.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();
//Console.WriteLine("-------------------------\n");
//DerivedClass B = new DerivedClass();
//B.DoWork(); // Calls the new method.
///*BaseClass A = (BaseClass)B;
//A.DoWork(); // Also calls the new method.*/
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//c.newTest();
//b.newTest();
//p.newTest();
//c.describeMe();
//b.describeMe();
//p.describeMe();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//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");
//c1.DescribeCar();
//cc.DescribeCar();
//mv.DescribeCar();
//Console.WriteLine("\n-------------------------");
//Console.WriteLine("-------------------------\n");
//***************************************************************
********************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();
//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);
}
}
}
//***************************************************************Polymorphism
examples
//***************************************************************************
*********
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine("Just a car.");
}
}
//***************************************************************Abstract
example
//***************************************************************************
****
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; }
}
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
int Volume
{
get;
set;
}
int cylNo();
}
int Volume
{
get;
set;
}
int cylNo();
}