Guest User

Guest User

  • Tech Writer
  • 48
  • 11.6k

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()   
  3.    
  4. Class Car : Vehicle  
  5.   Car()   
  6.   OpenTop(bool isConvertible)   
  7.    
  8. Class Truck : Vehicle  
  9.   Truck()   
  10.   PullTrailer(bool hasTrailerHitch)  
  11.    
  12. Main  
  13.   Vehicle car = new Car();  
  14.   Vehicle truck = new Truck();  
  15.    
  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();
and
Truck truck = new Truck();
 
And if I do that, will it prevent me from using anything from Vehicle? 
 
 

Answers (1)