Polymorphy Problem

M

mfunkmann

Hi there,

I was trying to pogram a class hierachy, and it worked great until I
was trying to implement GetModel, for some reasons I can't acess it in
the main program. But if I add the GetModel-function in the Auto
Object it suddenly it suddelny works, I am not sure want went wrong
there..


Thanks in advance

Mattias


class Program
{
abstract class Fahrzeug
{
protected int _raeder;

protected int Raeder
{
get { return _raeder; }
set { _raeder = value; }
}

protected int _ps;


protected int Ps
{
get { return _ps; }
set { _ps = value; }
}

public Fahrzeug(int p, int r)
{
_ps = p;
_raeder = r ;
}

public virtual void Startengine()
{
System.Console.WriteLine("Brumm Brummm");


}



}

class Auto:Fahrzeug
{
public Auto(int p, int r): base(p, r) {}


}

class Sportwagen : Auto
{

public string GetModel()
{
return ("ZX-12");
}
public Sportwagen(int p, int r) : base(p, r) { }
public override void Startengine()
{
System.Console.WriteLine("Sportwagen Start!");
}


}

interface Turbo
{

string GetModel();

}
static void Main(string[] args)
{
Auto f = new Auto(2,12);
f.Startengine();

Auto SW = new Sportwagen(2, 12);
SW.Startengine();
System.Console.WriteLine(SW.GetModel);

// error: Error 1 'ConsoleApplication4.Program.Auto' does not contain
a definition for 'GetModel' C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\Projects
\ConsoleApplication4\ConsoleApplication4\Program.cs 81 41
ConsoleApplication4





}
}
 
R

Richard A. Lowe

Hi Mattias,

The 'SW' variable in your program is of type 'Auto'. You try to call
GetModel() on this variable whose type, Auto, *does not* contain a method
called GetModel(). It does not matter that the object this variable points
to has a 'GetModel' method, Auto does not expose this method, so it's not
available unless you have a reference that is of the derived type which does
have that method exposed.

So, you could solve your issue by down casting to the derived type
'Sportwagen' like so:
((Sportwagen )SW).GetModel();

Hope this helps,
Richard
 
J

John B

Hi there,

I was trying to pogram a class hierachy, and it worked great until I
was trying to implement GetModel, for some reasons I can't acess it in
the main program. But if I add the GetModel-function in the Auto
Object it suddenly it suddelny works, I am not sure want went wrong
there..
.....
Auto SW = new Sportwagen(2, 12); .....
System.Console.WriteLine(SW.GetModel);
You are trying to access Auto.GetModel (should have '()' after call too)
but it is declared in Sprtwagen
You need to cast in this case to sportwagen,
((Sportwagen)SW).GetModel()
will work.

JB
 
J

Jeff Louie

As Richard and John have said you have declared SW as a reference
variable of
type Auto not Sportswagon. True, an object of class Sportswagon was
created,
but you do not actually have access to all the methods of the class
Sportswagon.
Instead you have a reference variable SW of type Auto so that only the
methods/
properties of type Auto are touchable with this variable. One solution
is to cast
the reference variable to type Sportswagon. Or you can just change the
declaration to:

Sportswagon SW= new Sportswagon(...

In a nutshell, when you declare a reference variable, the type of the
reference
variable restricts access to one of the object's public contracts.

http://www.geocities.com/jeff_louie/OOP/oop6.htm

Regards,
Jeff
 
M

mfunkmann

As Richard and John have said you have declared SW as a reference
variable of
type Auto not Sportswagon. True, an object of class Sportswagon was
created,
but you do not actually have access to all the methods of the class
Sportswagon.
Instead you have a reference variable SW of type Auto so that only the
methods/
properties of type Auto are touchable with this variable. One solution
is to cast
the reference variable to type Sportswagon. Or you can just change the
declaration to:

Sportswagon SW= new Sportswagon(...

In a nutshell, when you declare a reference variable, the type of the
reference
variable restricts access to one of the object's public contracts.

http://www.geocities.com/jeff_louie/OOP/oop6.htm

Regards,
Jeff

*** Sent via Developersdexhttp://www.developersdex.com***


Thanks a lot, I should have seen that, such a silly mistake by myself..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top