SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Constructors in Object-Oriented Programming: Overloading and State Management

chad avatar
21 diapositives · 357 Views · Jul 30, 2014

This chapter delves into constructors in object-oriented programming, focusing on their creation, overloading, and the critical role they play in object state management. It covers various aspects, such as the implementation of methods for complex numbers, including conjugation, addition, and multiplication, and addresses how to store state effectively. The chapter also emphasizes the significance of encapsulation and how to manage internal states of classes. Finally, it provides insights into overriding the `toString()` method for better object representation.

Understanding Constructors in Object-Oriented Programming: Overloading and State Management - slide 1
01 / 21Understanding Constructors in Object-Oriented Programming: Overloading and State Management
1 / 17

Télécharger la présentation

Understanding Constructors in Object-Oriented Programming: Overloading and State Management
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Chapter 5Constructors Constructors

  2. 1 Constructors Constructors

  3. 2 Overloading Constructors

  4. 3 ... Constructors

  5. 4 Problem Constructors

  6. 5 Solution Constructors

  7. 6 Scope Constructors

  8. 7 Object variables Constructors

  9. Complex conjugate 8 Example public complexNumber conjugate() { complexNumber conj = new complexNumber(new cartesian(this.x,-this.y); return conj; } add public complexNumber add (complexNumber operand) { double RealSum = this.x + operand.x; double ImSum = this.y + operand.y; return new complexNumber(new cartesian(RealSum, ImSum)); } Do I need to expose the internal state Constructors

  10. multiply 9 Private public complexNumber mult (complexNumber operand) { double RealProd = this.Real()*operand.Real() - this.Imaginary()+operand.Imaginary(); double ImProd = this.Real()*operand.Imaginary() + this.Imaginary()*operand.Real(); return new complexNumber(new cartesian(RealProd, ImProd)); } Here I am protecting one part of a class from changes in another part. multiply works if the storage of state change takes longer to execute add needs changing if the state changes executes fast Constructors

  11. 10 Storing state How to store state? public class complexNumber { private polar point; … } public class complexNumber { private cartesian point; … } How to decide? Constructors

  12. 11 Printing out an object public String toString() { String Cn = "(" + x + ", " + y + "i)"; return Cn; } The output from System.out.println(objectName); Is whatever is returned by toString; override toString in the Object class Constructors

  13. 12 Recap Constructors

  14. 13 Inheritance Constructors

  15. 14 ... Constructors

  16. 15 Extends Constructors

  17. 16 Core API Constructors

  18. 17 Pillage then burn Constructors

  19. 18 Evolution Constructors

  20. 19 Access Constructors

  21. 20 Object class Constructors