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

  • Context: MHB 
  • Thread starter Thread starter tmt1
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary
SUMMARY

In object-oriented programming (OOP), a penguin can be classified as a subtype of bird due to its specialized characteristics, despite lacking the ability to fly, which is a common attribute of birds. Conversely, a motorcycle cannot be classified as a subtype of a car, even though both share the drive method, because a motorcycle does not fulfill the "is a" relationship with cars. Instead, both motorcycles and cars are subtypes of MotorizedVehicle, which accurately represents their shared functionality. This distinction highlights the importance of proper class hierarchy and relationships in OOP design.

PREREQUISITES
  • Understanding of object-oriented programming principles
  • Familiarity with class hierarchies and inheritance
  • Knowledge of polymorphism and subtype relationships
  • Experience with programming languages that support OOP, such as Java or C#
NEXT STEPS
  • Research the principles of class inheritance in Java
  • Learn about polymorphism in C# and its practical applications
  • Explore design patterns that utilize subtype relationships
  • Study the SOLID principles of object-oriented design
USEFUL FOR

Software developers, particularly those working with object-oriented programming, and educators teaching OOP concepts will benefit from this discussion.

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.
 

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
3K
  • · Replies 6 ·
Replies
6
Views
3K