Boris G

Boris G

  • 1.9k
  • 87
  • 3k

Derived class method without matching base class method?

Oct 9 2019 1:24 PM
I would like to have methods that are unique to a sub-class. Visual Studio doesn't like this and won't let me compile. Is it possible to have a method in a derived class without it being in the base? Consider the following pseudocode:
  1. abstract Class Vehicle  
  2.   Vehicle()   
  4. Class Car : Vehicle  
  5.   Car()   
  6.   OpenTop(bool isConvertible)   
  8. Class Truck : Vehicle  
  9.   Truck()   
  10.   PullTrailer(bool hasTrailerHitch)  
  12. Main  
  13.   Vehicle car = new Car();  
  14.   Vehicle truck = new Truck();  
  16.   car.OpenTop(true);  // It doesn't like this.  
  17.   car.PullTrailer(false); // Or this.  
From the error it looks like the program expects both OpenTop and PullTrailer to appear in Vehicle, which I could, but I believe that I have been able to do something similar to this in past code, so I'm not sure what I'm missing. Must I declare the car and truck as:
Car car = new Car();
Truck truck = new Truck();
And if I do that, will it prevent me from using anything from Vehicle? 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (2)