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
Click For 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
Replies
3
Views
12K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K