Curso POO Teoria #12a - Conceito Polimorfismo (Parte 1)
Polymorphism in Object-Oriented Programming
Introduction to Polymorphism
- The lesson begins with Gustavo Guanabara introducing the concept of polymorphism, marking it as a significant topic in object-oriented programming (OOP).
- This is the final pillar of OOP, following encapsulation and inheritance, indicating that the course is nearing completion.
Understanding Polymorphism
- Polymorphism can be confusing due to its name; it combines "poli" (many) and "morpho" (forms), meaning many forms of doing something.
- An everyday example illustrates this: waking up can happen in various ways depending on the day, showcasing multiple methods for a single action.
Conceptual Definition
- According to the provided literature, polymorphism allows one name to represent different behaviors or methods.
- The example of waking up demonstrates how one method can have multiple implementations based on context.
Method Signatures
- Before delving deeper into technical aspects, it's essential to understand method signatures—how they are defined by parameter types and quantities.
- A method's signature includes the number and type of parameters it accepts; this distinction is crucial for understanding polymorphism.
Examples of Method Signatures
- An example shows a public method
calcularMediathat takes two real numbers as parameters and returns a real value.
- Another version of
calcularMediaalso takes two parameters but returns an integer instead. Despite differing return types, both methods share the same signature because they accept two real parameters.
Clarifying Signature Differences
- The discussion emphasizes that even if parameter names differ (e.g., N1 vs. V1), what matters for signatures is only their quantity and type.
- A new example introduces another version of
calcularMedia, which raises questions about whether it shares a signature with previous examples.
Understanding Method Signatures and Polymorphism
Differentiating Method Signatures
- The third method signature differs from the first two, as it accepts three parameters: an integer and two real numbers. This distinction highlights the importance of parameter types in defining method signatures.
- The fourth example introduces a method that calculates an average with four real number parameters, showcasing how varying the number of parameters leads to different signatures.
- A final example illustrates a method that receives an integer (bimestre) and returns a character, further emphasizing the diversity in method signatures among different examples.
Introduction to Polymorphism
- With an understanding of method signatures established, the discussion transitions to polymorphism, which is categorized into various types. The course will focus on two primary forms: overriding and overloading.
- Overriding polymorphism is highlighted as one of the most commonly used forms. It was briefly introduced in previous lessons through commands related to this concept.
Exploring Overriding Polymorphism
- The lesson emphasizes focusing on overriding polymorphism while acknowledging another form known as overloading, which is less common but still useful.
- An abstract class named "Animal" serves as a foundational example for illustrating polymorphism. This class encompasses various specializations such as mammals, reptiles, fish, and birds.
Attributes and Methods in Animal Class
- The "Animal" class includes attributes like weight, age, and number of limbs (e.g., four limbs), along with three abstract methods: feeding, locomotion, and sound emission.
- Each animal type inherits from the "Animal" class. For instance:
- Mammals have an additional attribute for fur color.
- Reptiles include scale color.
- Fish possess both scale color and a unique behavior (bubbles).
- Birds are characterized by feather color and nesting behavior.
Implementation Details
- The instructor plans to display both a diagram representing these classes alongside corresponding code snippets for clarity on implementing these concepts practically.
- Emphasis is placed on creating an abstract "Animal" class due to its inclusion of abstract methods; thus it cannot be instantiated directly but serves solely for inheritance purposes.
Object-Oriented Programming Concepts in Animal Classes
Defining the Mammal Class
- The
Mammalclass extends from theAnimalclass, inheriting attributes like weight, age, and limbs. It requires implementation of three abstract methods.
- The
Mammalclass adds an additional attribute: fur color. It does not have any extra methods beyond those inherited fromAnimal.
Implementing Abstract Methods for Mammals
- The method
locomoveris overridden to define that all mammals move by running, which will print "running" when called.
- The method
alimentaris overridden to indicate that mammals feed by nursing during their early life stages.
- The method
emitir somis also overridden to generically output "mammal sound" when invoked.
Creating the Reptile Class
- A new class called
Reptile, extending fromAnimal, is introduced with an additional attribute: scale color.
- The locomotion method for reptiles is defined as crawling, while their feeding behavior involves eating plants.
Developing the Fish Class
- A new class named
Fish, which also extends fromAnimal, includes an additional attribute: scale color.
- Fish are defined to swim as their mode of locomotion and eat substances found in water. An additional method,
soltar bolha, indicates that fish can release bubbles.
Introducing the Bird Class
- Another subclass called
Birdinherits fromAnimal, adding a feather color attribute.
- Birds fly as their primary means of movement and typically feed on fruits. They emit a generic bird sound and have a unique method for building nests.
Main Program Implementation
- An example program begins with object instantiation; however, creating an instance of the abstract class
Animaldirectly is invalid.
- Valid objects are created for each subclass: mammal (M), reptile (R), fish (P), and bird (A).
Method Calls on Objects
- For the mammal object M, attributes such as weight (85.3), age (2 years), and members (4 limbs) are set using setter methods.
- Calling methods on object M results in outputs reflecting its behaviors: "running," "nursing," and "mammal sound."
Polymorphism in Object-Oriented Programming
Understanding Object Behavior through Polymorphism
- The discussion begins with the creation of a "fish" object, which can perform actions like swimming, eating substances, and emitting sounds. Each action corresponds to specific methods defined for the fish class.
- An "bird" object is introduced with attributes such as weight (0.89), age, and limbs. It can also perform actions like flying, eating fruits, making bird sounds, and nesting.
- The speaker emphasizes that calling the same methods (locomover, alimentar, emitir som) on different objects (mammal vs. fish) yields different results due to their unique implementations.
- This variation in method responses despite identical names illustrates polymorphism—where a single method name can invoke different behaviors based on the object's class.
- The concept of polymorphism is further clarified as performing an action under the same name but allowing for diverse outcomes depending on the object's type.
Types of Polymorphism
- The speaker explains "method overriding," where subclasses replace superclass methods while maintaining the same signature. This allows for tailored behavior in derived classes like mammals and birds.
- Method signatures are highlighted; all relevant methods share zero parameters, reinforcing that they belong to the same signature category necessary for polymorphic behavior.
Class Hierarchies and Inheritance
- A visual representation of class hierarchies is discussed. Classes such as kangaroo and dog inherit from mammals; snakes and turtles inherit from reptiles; goldfish from fish; and macaws from birds.
- Each subclass retains characteristics from its parent class while introducing specialized features—e.g., kangaroos have pouches while dogs can bury bones.
Implementing Specific Behaviors
- The kangaroo's ability to use its pouch is noted alongside its locomotion method being overridden to reflect jumping instead of running—a key example of how subclasses modify inherited behaviors.
- As new classes are created (like kangaroo and dog), each will implement specific functionalities that differentiate them within their respective categories while still adhering to overarching animal traits.
Finalizing Class Implementations
- Implementation details for both kangaroo and dog classes are provided. Both extend mammal functionality but introduce unique behaviors: jumping for kangaroos and burying bones or wagging tails for dogs.
Understanding Inheritance and Polymorphism in Object-Oriented Programming
Overview of Class Implementation
- The diagram illustrates that the classes "Cobra," "Goldfish," and "Arara" do not implement any unique methods, indicating they are empty and only inherit properties from their superclass.
- The "Tartaruga" class overrides the locomotion method, as reptiles typically crawl, but turtles move slowly on land. This demonstrates method overriding in action.
Main Program Example
- An example program is introduced with a declared object of type "Mamífero" (Mammal), along with its subclasses: "Canguru" (Kangaroo) and "Cachorro" (Dog). This sets up a practical demonstration of inheritance.
- The mammal object is initialized with specific attributes such as weight, age, number of limbs, locomotion method ("running"), feeding method ("nursing"), and sound emission ("mammal sound").
Subclass Behavior
- A kangaroo object is created with distinct attributes. When invoking the locomotion method for the kangaroo, it uses its overridden jumping behavior instead of running like a typical mammal.
- The concept of polymorphism is highlighted; while both mammals and kangaroos share common traits, their locomotion methods differ due to overriding.
Method Overriding Insights
- When calling the feeding method for the kangaroo, it defaults to the inherited behavior from the mammal class since it hasn't been overridden. This emphasizes how subclassing works when methods are not explicitly defined in subclasses.
- A dog object is also created. It utilizes inherited behaviors for locomotion (running), feeding (nursing), and sound emission (mammal sound), showcasing that not all subclasses need to override every method.
Practical Application Discussion
- There’s an opportunity for students to choose a programming language (PHP or Java) to implement concepts discussed in this lesson. This encourages hands-on practice following theoretical understanding.
- Students are directed to find corresponding lessons in playlists based on their chosen programming language for further learning about object-oriented programming principles.