Here, I am trying to explain the use of
the constructor in the abstract class.
I am Considering Shape as the abstract class and Square and rectangle as the derived
class.
public
abstract class
Shape
{
protected
double x = 10;
protected
double y = 10;
protected
Shape()
{ }
protected
Shape(double x, double
y)
{
this.x
= x;
this.y
= y;
}
public
abstract double
AreaClaculate();
}
public
class square
: Shape
{
public
square()
{ }
public
override double
AreaClaculate()
{
return
x * y; // here x=10 and y=10
works, i set x=y due to square, if any we have any other shape other than square
then x!=y
}
}
public class
rectangle :
Shape
{
public
rectangle(double x,
double y)
:
base(x, y)
// it will call Shape(double x, double y) at base class
{
}
public
override double
AreaClaculate()
{
return
x * y; ///
hre x and y will pass from runtime
}
}
static
void Main(string[]
args)
{
square s =
new square();
double
areasq= s.AreaClaculate(); //
we will get 100, because x=y=100 and x*y=100
rectangle r =
new rectangle(10,5);
// behind the scene 10,5 are passing to
shape(double x, double y) at base class
double
arearect = r.AreaClaculate();
// we will get 50.
}
MUKESH SHARMAPosted Sep 1, 2015, 6:47 AM
good sir
praveen DeshamPosted Oct 29, 2014, 7:40 AM
program Class derived from an abstract class must implement all the abstract members of parent abstract class A "public abstract double area()"
JUKE BOXPosted Oct 16, 2014, 1:05 PM
when i use above program ..it gave exception as...program doesnot implement inherited abstract member...can u please sort out this problem???
JUKE BOXPosted Oct 16, 2014, 1:03 PM
using System; namespace abstractclassconntruct { public abstract class A { protected double x = 10; protected double y = 7; public A() { } public A(double x, double y) { this.x = x; this.y = y; } public abstract double area(); } public class square : A { public square() { } public override double area() { return x * x; } } public class rectangle:A { public rectangle(double x,double y): base (x,y) { } public override double area() { return x * y; } } class program : A { static void Main(string[] args) { square s = new square(); double areacal = s.area(); rectangle r = new rectangle(10, 5); double areaca = r.area(); Console.ReadKey(); } } }