Home All Groups Group Topic Archive Search About

Interfaces and Abstract Classes

Author
3 Jan 2007 7:50 PM
stephen
Hi all,

I have an interface
interface ICar
{
void accelerate(double amt);
void brake(double amt);
double getSpeed();
}

and I create an abstract class
public abstract class GenericCar : ICar
{
protected double _speed;

public abstract void accelerate(double amt);

sealed override public void brake( double amt )
{
  if( ( _speed - amt ) < 0.0 )
  {
   _speed = 0.0;
   return;
  }
  _speed -= amt;
}

sealed override public getSpeed()
{
  return _speed;
}
}

Class TestCar : GenericCar
{
//I want to have implementation for accelerate only
//and want to control brake and getSpeed from abstract class
}

I read on MSDN that If I have sealed override then I can access the abstract
classes methods
but it gives me an error "No Suitable method to override"

Please advice,
Stephen

Author
3 Jan 2007 8:03 PM
Brian Gideon
stephen wrote:
Show quote
> Hi all,
>
> I have an interface
> interface ICar
> {
>  void accelerate(double amt);
>  void brake(double amt);
>  double getSpeed();
> }
>
> and I create an abstract class
> public abstract class GenericCar : ICar
> {
>  protected double _speed;
>
>  public abstract void accelerate(double amt);
>
>  sealed override public void brake( double amt )
>  {
>   if( ( _speed - amt ) < 0.0 )
>   {
>    _speed = 0.0;
>    return;
>   }
>   _speed -= amt;
>  }
>
>  sealed override public getSpeed()
>  {
>   return _speed;
>  }
> }
>
> Class TestCar : GenericCar
> {
>  //I want to have implementation for accelerate only
>  //and want to control brake and getSpeed from abstract class
> }
>
> I read on MSDN that If I have sealed override then I can access the abstract
> classes methods
> but it gives me an error "No Suitable method to override"
>
> Please advice,
> Stephen

Stephen,

The sealed and override keywords do not affect access to the method.
You certainly can access a sealed override method from a concrete
class.  What you cannot do is redefine it's implementation since it is
marked as sealed.  The error message leads me to believe that you were
trying to override brake and getSpeed in TestCar.  If you want to
override those methods in TestCar then don't mark them as sealed in
GenericCar.

Brian
Author
4 Jan 2007 5:51 PM
stephen
Thanks Brian and Architect,

I read an article that uses New Keyword and I tried it and it worked but I
have to test whether you can override it in the derived class.

Thanks for the info,
Stephen


Show quote
"Brian Gideon" <briangid***@yahoo.com> wrote in message
news:1167854610.657967.46840@n51g2000cwc.googlegroups.com...
>
> stephen wrote:
>> Hi all,
>>
>> I have an interface
>> interface ICar
>> {
>>  void accelerate(double amt);
>>  void brake(double amt);
>>  double getSpeed();
>> }
>>
>> and I create an abstract class
>> public abstract class GenericCar : ICar
>> {
>>  protected double _speed;
>>
>>  public abstract void accelerate(double amt);
>>
>>  sealed override public void brake( double amt )
>>  {
>>   if( ( _speed - amt ) < 0.0 )
>>   {
>>    _speed = 0.0;
>>    return;
>>   }
>>   _speed -= amt;
>>  }
>>
>>  sealed override public getSpeed()
>>  {
>>   return _speed;
>>  }
>> }
>>
>> Class TestCar : GenericCar
>> {
>>  //I want to have implementation for accelerate only
>>  //and want to control brake and getSpeed from abstract class
>> }
>>
>> I read on MSDN that If I have sealed override then I can access the
>> abstract
>> classes methods
>> but it gives me an error "No Suitable method to override"
>>
>> Please advice,
>> Stephen
>
> Stephen,
>
> The sealed and override keywords do not affect access to the method.
> You certainly can access a sealed override method from a concrete
> class.  What you cannot do is redefine it's implementation since it is
> marked as sealed.  The error message leads me to believe that you were
> trying to override brake and getSpeed in TestCar.  If you want to
> override those methods in TestCar then don't mark them as sealed in
> GenericCar.
>
> Brian
>
Author
3 Jan 2007 8:26 PM
Architect
Please show the code where you get the error.

Regards,
Valentin Ivanov.
Show quote
"stephen" <stephen***@hotmail.com> wrote in message
news:%23WhNkB3LHHA.5104@TK2MSFTNGP06.phx.gbl...
>
> Hi all,
>
> I have an interface
> interface ICar
> {
> void accelerate(double amt);
> void brake(double amt);
> double getSpeed();
> }
>
> and I create an abstract class
> public abstract class GenericCar : ICar
> {
> protected double _speed;
>
> public abstract void accelerate(double amt);
>
> sealed override public void brake( double amt )
> {
>  if( ( _speed - amt ) < 0.0 )
>  {
>   _speed = 0.0;
>   return;
>  }
>  _speed -= amt;
> }
>
> sealed override public getSpeed()
> {
>  return _speed;
> }
> }
>
> Class TestCar : GenericCar
> {
> //I want to have implementation for accelerate only
> //and want to control brake and getSpeed from abstract class
> }
>
> I read on MSDN that If I have sealed override then I can access the
> abstract classes methods
> but it gives me an error "No Suitable method to override"
>
> Please advice,
> Stephen
>

AddThis Social Bookmark Button