MHB Why Can Penguins Be Subtyped as Birds But Motorcycles Not as Cars in OOP?

  • Thread starter Thread starter tmt1
  • Start date Start date
  • Tags Tags
    Programming
AI Thread Summary
In object-oriented programming, the classification of a penguin as a subtype of bird is acceptable despite its differing attributes, such as the inability to fly, because it aligns with the concept of inheritance where a penguin is a specialized version of a bird. This reflects the principle of "is-a" relationships in which subtypes share a common base class. Conversely, a motorcycle cannot be classified as a subtype of a car because, despite both having similar behaviors like driving, a motorcycle does not fit the "is-a" relationship with cars. Instead, both motorcycles and cars are better categorized under a broader class, MotorizedVehicle, which accurately represents their shared characteristics without misrepresenting their distinct identities.
tmt1
Messages
230
Reaction score
0
For object oriented programming, why is is acceptable to have a
penguin be a subtype of bird even though a penguin has different
attributes than a bird(supposing we have a fly method) and yet it is
unacceptable for for Motorcycle to be a subtype of Car even though it
has the same behaviour (ie drive method)?
 
Technology news on Phys.org
tmt said:
For object oriented programming, why is is acceptable to have a
penguin be a subtype of bird even though a penguin has different
attributes than a bird(supposing we have a fly method) and yet it is
unacceptable for for Motorcycle to be a subtype of Car even though it
has the same behaviour (ie drive method)?

Hi tmt!

Both Car and Motorcycle are specializations of a MotorizedVehicle.
That's the one that has for instance a drive method.
Put otherwise, we can't say that a Motorcycle "is a" Car, since that is just not true.

On the other hand, we can treat a penguin as a more specialized version of a bird.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top