Curso POO Teoria #10a - Herança (Parte 1)

Curso POO Teoria #10a - Herança (Parte 1)

Introduction to Inheritance in Object-Oriented Programming

Overview of Object-Oriented Programming Pillars

  • The instructor introduces the topic of inheritance as the second pillar of object-oriented programming (OOP), following encapsulation and abstraction.
  • Recap of the three pillars: Encapsulation, Inheritance, and Polymorphism. Emphasis on understanding these foundational concepts is crucial for grasping OOP.

Conceptualizing Inheritance

  • The instructor uses a relatable analogy involving a mother and daughter to explain inheritance, highlighting how children inherit traits from their parents.
  • Characteristics such as physical features (e.g., eyes, mouth) and behaviors (e.g., mannerisms) can be inherited from a parent class to a child class.

Understanding Class Relationships

Defining Classes and Subclasses

  • Inheritance allows new classes (subclasses) to be based on existing classes (parent classes), enabling reuse of code without starting from scratch.
  • Example provided with pens: A new pen can inherit characteristics from an existing pen while adding new features, illustrating practical application of inheritance.

Practical Implications of Inheritance

  • Both characteristics and behaviors are inherited; every parent class provides its attributes to its subclasses.

Examples in Practice

Class Examples in Context

  • Three distinct classes are introduced: Student, Teacher, and Staff. Each has unique attributes but shares common characteristics.
  • Attributes for each class include name, age, gender; specific functionalities like enrolling or teaching are also defined for each role.

Abstraction in OOP

Understanding Object-Oriented Programming Concepts

Characteristics and Class Structure

  • The speaker discusses the importance of identifying shared characteristics among different entities, such as students, teachers, and staff, to avoid redundancy in programming.
  • A new class is proposed that will encapsulate common attributes like name, age, and gender. This class is named "Pessoa" (Person).
  • The concept of inheritance is introduced; all derived classes (student, teacher, staff) will inherit properties from the "Pessoa" class.

Hierarchical Relationships in Classes

  • The speaker illustrates a hierarchy where subclasses inherit behaviors and characteristics from their parent class ("Pessoa"), similar to familial relationships.
  • Each subclass (student, teacher, staff) retains unique attributes while sharing common traits with the parent class. For example, a student has enrollment details but also inherits basic personal information.

Implementing Inheritance in Code

  • To implement this structure in code, the "Pessoa" class must be defined first as it serves as the foundation for other classes.
  • The attributes for the "Pessoa" class are established: name (string), age (integer), and gender (string). Methods like celebrating a birthday are also included.

Creating Subclasses: Student and Teacher

  • Following the creation of "Pessoa," attention shifts to defining the "Aluno" (Student) class with specific attributes like enrollment number and course.
  • The speaker emphasizes encapsulation by creating getter and setter methods for managing access to private data within these classes.

Clarifying Key Object-Oriented Principles

  • It’s clarified that encapsulation can exist independently of inheritance; both concepts do not rely on each other despite being fundamental pillars of object-oriented programming.
  • While encapsulation and inheritance can coexist harmoniously in software design, polymorphism is mentioned as a separate topic to be explored later.

Finalizing Class Definitions

  • To establish relationships between classes in code, it’s necessary to declare that subclasses extend from their parent class. For instance, “Aluno extends Pessoa” indicates that students inherit properties from persons.
  • Similar definitions are made for the "Professor" (Teacher), which includes additional attributes like specialty and salary while maintaining inherited features from "Pessoa."

Completing Employee Class Definition

  • Finally, an employee ("Funcionário") class is created with its own unique attributes while still extending from "Pessoa," ensuring all employees share common characteristics defined at a higher level.

Understanding Inheritance in Object-Oriented Programming

Key Concepts of Inheritance

  • The discussion introduces additional concepts related to inheritance, highlighting simple and complex terms within the context of object-oriented programming.
  • The speaker explains that "Pessoa" (Person) is the parent class, while "Aluno" (Student), "Funcionário" (Employee), and "Professor" (Teacher) are child classes. This mirrors real-life relationships between a mother and daughter.
  • Parent classes may also be referred to as progenitors or superclasses. Child classes can be called subclasses, which inherit attributes and behaviors from their parent class automatically.

Class Structure and Examples

  • An example program is introduced where four instances are created: two students, one teacher, and one employee. Each instance corresponds to different classes derived from the parent class.
  • Attributes such as name and age are inherited by subclasses like Aluno from Pessoa without needing explicit declaration in the subclass definition.

Methods and Attributes

  • The student class inherits methods like making a birthday celebration from the person class while having its own specific attributes such as enrollment number and course.
  • All methods defined in the parent class are accessible to child classes. For instance, an Aluno has access to methods for handling personal information alongside its unique functionalities.

Practical Application of Concepts

  • The speaker provides practical examples by assigning names to each instance created earlier, demonstrating how attributes can be set for each type of object based on their inherited properties.
  • Specific commands illustrate how unique attributes can be manipulated; for example, setting a course for a student or salary for a professor shows how these properties function within their respective contexts.

Limitations of Class Methods

  • Certain methods are exclusive to specific classes; for example, only professors have salary-related methods since they derive from the superclass but possess unique characteristics not shared with other subclasses.
  • Attempts to invoke inappropriate methods on instances lead to errors; e.g., trying to change an Aluno's job fails because it’s not applicable outside its defined role as a student.

Conclusion & Further Learning Opportunities

  • The speaker emphasizes understanding these principles through practice in chosen programming languages like PHP or Java.
Video description

Nessa aula de POO, vamos aprender o que é Herança em Programação Orientada a Objetos. 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/