Curso POO Teoria #11a - Herança (Parte 2)

Curso POO Teoria #11a - Herança (Parte 2)

Understanding Hierarchical Trees and Inheritance in Object-Oriented Programming

Concepts of Leaves, Descendants, and Ancestors

  • Classes without child classes are referred to as "leaves." For example, classes like aaa, abaa, abab, ABB, and aca are considered leaves since they do not have children.
  • In hierarchical trees, the concepts of descendants and ancestors are crucial. For instance, class a is the root; class c is a child of a. Class aca is a descendant of class a because it traces back through its parent.
  • To define a descendant, one does not count the parent itself. For example, both classes AB and aca are descendants of class a.
  • An ancestor must be above its descendants in the hierarchy. Thus, while class a is an ancestor to both classes aba and ABB, it cannot be an ancestor to its own direct children.
  • Traversing from top to bottom in a tree represents specialization (narrowing down), while traversing from bottom to top represents generalization (broadening). Understanding these terms is essential for object-oriented programming.

Types of Inheritance

  • The discussion transitions into practical examples based on previous lessons about inheritance types. Two main types will be covered: implementation inheritance and inheritance by difference.
  • Implementation inheritance (also known as "poor inheritance") is the simplest form where subclasses inherit attributes/methods from a superclass without adding new features.
  • Inheritance by difference is more comprehensive; it allows subclasses to inherit from a superclass while also introducing additional attributes or methods that enhance functionality.

Practical Example with Classes

  • A generic abstract class named "Person" includes attributes such as name, age, gender, and methods like celebrating birthdays. This serves as the foundation for other specific classes.
  • Three derived classes—Visitor, Student, and Teacher—inherit from the Person class. Each subclass retains inherited attributes while potentially adding their unique characteristics.
  • The Student subclass adds specific attributes like enrollment number and course details along with payment capabilities. Similarly, the Teacher subclass includes specialty areas and salary management features.

Understanding Implementation vs. Difference Inheritance

  • The Teacher inherits all basic attributes from Person but also has specialized methods such as receiving raises alongside standard ones like celebrating birthdays.
  • The Visitor subclass exemplifies implementation inheritance; it does not add any new attributes or methods beyond what it inherits from Person—hence termed "poor inheritance."

Understanding the Differences Between Student Types

Characteristics of Different Student Types

  • The distinction between scholarship students, technical students, and regular students is discussed. Both scholarship and technical students possess all student characteristics such as enrollment, course registration, and tuition payment.
  • Scholarship status is described as a specialization within the broader category of "student." This includes understanding how scholarships add value and the methods for renewing them or paying tuition.
  • Technical students are noted to have professional registration as an attribute and must engage in practical activities. The discussion emphasizes that these concepts can be condensed into a general understanding of "student."

Key Programming Concepts: Abstract vs Final

  • The instructor introduces abstract and final concepts in programming, emphasizing their importance in object-oriented programming (OOP). These terms will be crucial for learners to grasp.
  • Four key concepts are outlined: abstract class, abstract method, final class, and final method. Understanding these distinctions is essential for effective programming.
  • An abstract class cannot be instantiated; it serves only as a parent class. This principle applies across all OOP languages.

Methods Explained: Abstract vs Final

  • An abstract method is defined but not implemented in its parent class. It can only exist within an interface or an abstract class.
  • A final class cannot be inherited by other classes; it acts as a terminal point in inheritance hierarchies—referred to metaphorically as a "sterile" class with no children.
  • A final method cannot be overridden by subclasses; it must remain intact when inherited. This concept will be further explored later in the lesson.

Importance of Understanding Key Terms

  • Students are encouraged to take screenshots of important definitions since they will frequently appear in exercises. Familiarity with these terms is vital for mastering various programming languages beyond just PHP and Java.
  • Repeatedly reviewing conceptual lessons is recommended to solidify understanding over time. Creating playlists for conceptual videos may aid retention.

Practical Application: Building Code Examples

  • The session transitions into coding practice where the instructor begins constructing code based on previously discussed diagrams, starting with an abstract class named "Person."
  • Attributes like name, age, and gender are set as private to maintain encapsulation principles learned earlier. Getter and setter methods will also need to be included due to private attributes.
  • A final method called "celebrateBirthday" is introduced within the Person class that cannot be overridden later on—a practical demonstration of previous theoretical discussions about final methods.

Understanding Inheritance in Object-Oriented Programming

Class Visitor and Inheritance

  • The "Visitor" class is introduced as a simple implementation that inherits from the "Person" class, automatically acquiring its attributes and methods.
  • The "Visitor" class includes attributes like name, age, sex, and a final method for celebrating birthdays. This method is inherited without being overridden.
  • Since "Visitor" is not an abstract class but inherits from an abstract one ("Person"), it can be instantiated without issues.

Class Student (Aluno)

  • The "Student" class also extends the "Person" class, adding specific attributes such as enrollment number and course along with a method to pay tuition fees.
  • Two types of inheritance are discussed: implementation inheritance (for Visitor) and difference inheritance (for Student).

Class Scholar (Bolsista)

  • The "Scholar" class extends the "Student," inheriting all its properties while also including additional features like scholarship amount and a method to renew scholarships.
  • Scholars inherit everything from Students and indirectly from Persons. They have unique payment methods for tuition fees which differ from regular students.

Polymorphism in Action

  • The concept of overriding methods is introduced; the scholar's payment method differs from that of a student, showcasing polymorphism.
  • Three pillars of object-oriented programming are highlighted: encapsulation, inheritance, and polymorphism. Each plays a crucial role in structuring classes effectively.

Practical Application

  • Instantiation examples are provided; however, instantiating an abstract Person class fails since it cannot be directly created.
  • Emphasis on understanding theoretical concepts before moving to practical applications is advised for better comprehension.

Learning Resources

  • Viewers are encouraged to choose between PHP or Java for practical lessons on these concepts.
Video description

Nessa aula de POO, vamos aprender como funcionam os tipos de Herança, que são a Herança de Implementação e Herança para Diferença. Além disso, vamos ver algumas nomenclaturas importantes para a Programação Orientada a Objetos. Outros conceitos abordados nessa aula são: super-classe, sub-classe, ancestral, descendente, classe raiz, classe folha, generalização, especialização, classe abstrata, classe final, método abstrato e método final. Gostou da aula? Então torne-se um Gafanhoto APOIADOR do CursoemVídeo acessando o site apoie.me/cursoemvideo Nós do CursoemVideo sempre recomendamos assistir a aula completa, mas se quiser aprender diretamente uma parte específica, clique nos marcadores de tempo a seguir: 0:19 - Qual é o assunto da aula? Aula do Curso de Programação Orientada a Objetos POO criado pelo professor Gustavo Guanabara para o portal CursoemVideo.com. Curso em Vídeo Seja um apoiador: http://apoie.me/cursoemvideo Site: http://www.cursoemvideo.com YouTube: http://www.youtube.com/cursoemvideo Facebook: http://www.facebook.com/cursosemvideo Twitter: http://twitter.com/cursosemvideo Google+: http://plus.google.com/112666558837414979080 Patrocínio HOSTNET: http://www.hostnet.com.br EDUCANDUS: http://www.sistemaeducandus.com.br GAFANHOTOS: http://apoie.me/cursoemvideo Ask Rufus de Audionautix está licenciada sob uma licença Creative Commons Attribution (https://creativecommons.org/licenses/by/4.0/) Artista: http://audionautix.com/