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

  • MHB
  • Thread starter tmt1
  • Start date
  • Tags
    Programming
In summary, object oriented programming allows for penguins to be a subtype of bird even though they have different attributes, while it is not acceptable for a Motorcycle to be a subtype of Car despite having the same behavior. This is because a penguin is a specialized version of a bird, while a Motorcycle is not necessarily a specialized version of a Car.
  • #1
tmt1
234
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
  • #2
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.
 

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

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm that focuses on creating objects that contain data and code, and interact with one another. It allows for the creation of modular, reusable, and maintainable code.

What are the main principles of Object Oriented Programming?

The main principles of Object Oriented Programming are encapsulation, inheritance, and polymorphism. Encapsulation refers to the bundling of data and code within an object, inheritance allows for the creation of new classes based on existing ones, and polymorphism enables objects to take on different forms and behaviors.

What are the advantages of using Object Oriented Programming?

Using Object Oriented Programming can lead to improved code organization, reusability of code, and easier maintenance. It also allows for the creation of complex applications by breaking them down into smaller, more manageable objects.

What is the difference between a class and an object in Object Oriented Programming?

A class is a blueprint or template for creating objects, whereas an object is an instance of a class. In other words, a class defines the properties and behaviors of an object, while an object is a specific realization of a class.

What programming languages support Object Oriented Programming?

Many programming languages support Object Oriented Programming, some of the most popular ones include Java, C++, Python, and Ruby. It is also possible to use OOP principles in languages that are not inherently object oriented, such as JavaScript.

Back
Top