Create inherited class from base class
Posted
by Raj
on Stack Overflow
See other posts from Stack Overflow
or by Raj
Published on 2010-05-20T16:20:33Z
Indexed on
2010/05/20
16:30 UTC
Read the original article
Hit count: 457
public class Car
{
private string make;
private string model;
public Car(string make, string model)
{
this.make = make;
this.model = model;
}
public virtual void Display()
{
Console.WriteLine("Make: {0}", make);
Console.WriteLine("Model: {0}", model);
}
public string Make
{
get{return make;}
set{make = value;}
}
public string Model
{
get{return model;}
set{model = value;}
}
}
public class SuperCar:Car
{
private Car car;
private int horsePower;
public SuperCar(Car car)
{
this.car = car;
}
public int HorsePower
{
get{return horsePower;}
set{horsepower = value;}
}
public override void Display()
{
base.Display();
Console.WriteLine("I am a super car");
}
When I do something like
Car myCar = new Car("Porsche", "911");
SuperCar mySupcar = new SuperCar(myCar);
mySupcar.Display();
I only get "I am a supercar" but not the properties of my base class. Should I explicitly assign the properties of my base class in the SuperCar constructor? In fact I'm trying Decorator pattern where I want a class to add behaviour to a base class.
© Stack Overflow or respective owner