Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie inheritance question

Status
Not open for further replies.

newtoASP

Programmer
Jun 1, 2001
40
US
An example in the book "Programming VB.net: a guide for experienced programmers" (pg. 196), deals with abstract base classes. A PayableEntity class has a 'MustOverride Public' member called TaxID(). The implementation is left to the Contractor class and the Employee class. My question is: why should there even be a member in the base class, is it needed? If you have to put the code into each class that inherits from the PayableEntity class, what is the benefit?

Thanks in advance.
 
Hey newToASP,

To keep with the OOP paradigm, you always include the generic functionality that will comprise the base object. The base object will NEVER be created. However, the traits of derived objects MUST be present for them to be considered as derivations of the base class...

Man, that sounds stupid. Let me put it this way:
You have a base class TRANSPORT.
All transport objects, trains, planes, autos, etc, get you from point A to point B, so the base object TRANSPORT will have a Travel() method. The way, the IMPLEMENTATION, of how a train goes between a and b, and the way a car goes between a and b, are very different. But, since both are forms of Trasportation, they both must contain the Travel method.

So, in short, the member in the base class is needed.

I hope that helps.

mike
 
Thanks Mike,
So, if the AUTO class had a method changetire()(or some method specific to auto's) would that have to be present in the base class also, given that not all of the other forms of TRANSPORT need it? If so, would the other classes that inherit from TRANSPORT need that method? Sorry for the basic questions, just trying to get a handle on this OOP stuff.
-Greg

 
No, because a boat doesn't have a tire to change.

Look at it this way.

AUTO is a type of transport.
Car and MotorBike are types of AUTO.
The ChangeTire() method is unique to AUTOmobiles, for this example.
Both the Car and the MotorBike are going to override the ChangeTire() method, because the details of changing a tire for a 4 wheel car and a 2 wheel motorbike are different.

Now, back to TRANSPORT.

TRANSPORT is going to contain classes and methods that are REQUIRED by all of its descendants. For example : maxpassengers, maxfuel, FuelType, AddFuel(), GetCurrentFuelAmmount(), AddPassenger(), etc.
Every form of TRANSPORT has these things. These traits and methods define transport. Everything that is derived from transport (thus being a transport itself), must have these methods.

Now to the specific problem of the PayableEntity (PE). Every PE must have a TaxID. The implementation of TaxID() is dependent on the descendant. But in the same way that any AUTO must be able to ChangeTire(), every PE must have TaxID, and in the same way that Car and MotorBike implement the ChangeTire() differently due to inherently different details, each child of PE must implement TaxID() based on their own details.

At some level, it is a matter of preference. It depends on who is defining the base objects, the criteria, etc. Ideally, we would all be careful enough and thorough enough to come up with the same base objects and be able to maintain 100% code re-use, but that isn't the case.

Hope this makes things a bit clearer.

mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top