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:
- abstract Class Vehicle
- Vehicle()
- Class Car : Vehicle
- Car()
- OpenTop(bool isConvertible)
- Class Truck : Vehicle
- Truck()
- PullTrailer(bool hasTrailerHitch)
- Main
- Vehicle car = new Car();
- Vehicle truck = new Truck();
- car.OpenTop(true); // It doesn't like this.
- 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?
Dinesh GabhanePosted Oct 11, 2019, 7:34 AM