Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
C# example (Polymorphesim)
1. Example In C#
Shows How Object Oriented
Is
Classes/Polymorphesim
Inherentence/Overloading
& Overriding
Done By:-
MARWA SAMIH AL-AMRI
2. using System;
using System.Collections.Generic;
using System.Text;
namespace uml
{
class Shape
{
public virtual void draw()
{
Console.WriteLine("Empty");
}
}
{
using System;
using System.Collections.Generic;
using System.Text;
namespace uml
{
class Circle:Shape
{
public override void draw()
{
Console.WriteLine("This is Circle");
}
public double m (int x,int y)
{
return x * y;
}
public double m(int x, double y, int z)
{
Console.WriteLine("nnthe integer version = "+ m(1,2));
Console.WriteLine("the double version = " + m(2.3 , 2));
Console.WriteLine("with different order = " + m(2, 2.3));
return x * y / z;
}
public double m(double x, int y)
{
return x * y;
}
public double m(int x, double y)
{
return x / y;
}
}
}
3. using System;
using System.Collections.Generic;
using System.Text;
namespace uml
{
class Rectangle:Shape
{
public override void draw()
{
Console.WriteLine("This is Rectangle");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace uml
{
class Square:Rectangle
{
public override void draw()
{
base.draw();
Console.WriteLine("this is Square");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace uml
{
class Program
{
static void Main(string[] args)
{
Shape S;
Rectangle R = new Rectangle();
S = R;
S.draw();
S = new Circle();
4. S.draw();
//S.printCenter(); error
Circle C = new Circle();
C.printCenter();
S = new Square();
S.draw();
R = new Square();
S = R;
S.draw();
//R = S; error
Console.WriteLine("n");
Shape [] sh = new Shape[4];
sh[3] = new Shape();
sh[0] = new Rectangle();
sh[1] = new Circle();
sh[2] = new Square();
for (int i = 0; i < 4; i++)
sh[i].draw();
}
}
}