PRIVATE AND PUBLIC
ATTRIBUTES AND
METHODS
MARIO B. CADAY
INSTRUCTOR I
INTRODUCTION TO OBJECT-ORIENTED
PROGRAMMING (OOP)
Main Points:
 OOP is a programming paradigm based on the concept of objects.
Objects represent real-world entities and are defined by attributes (data)
and methods (functions).
WHAT ARE ATTRIBUTES AND METHODS?
 Attributes (Fields or Properties): Variables that hold data about the object.
 Methods (Functions): Actions or behaviors that an object can perform.
PUBLIC ATTRIBUTES AND METHODS
• Definition: Public members are accessible from outside the class.
• Use Case: Good for attributes or methods that need to be accessed by other
classes or modules.
• Syntax: Typically, no special symbol is required to declare something public.
PRIVATE ATTRIBUTES AND METHODS
 Definition: Private members are restricted to the class where they are
defined.
 Use Case: Used to hide implementation details and protect the integrity of
data.
 Syntax: In Python, prefix an attribute or method with two underscores __ to
make it private.
DIFFERENCES BETWEEN PUBLIC AND PRIVATE
MEMBERS
 Access:
 Public members can be accessed by any part of the code.
 Private members can only be accessed within the class they are defined in.
Example Comparison:
 Public method can be called from outside: car.drive()
 Private method can’t be accessed directly: car.__drive() would raise an error.
WHY USE PRIVATE MEMBERS?
Main Points:
Encapsulation: Protects data by controlling access.
Data Integrity: Prevents accidental modification.
Abstraction: Hides the complexity of the internal
workings of a class.
Security: Secures sensitive information.
GETTER AND SETTER METHODS
• Purpose: Provide controlled access to private attributes through public
methods.
REAL-WORLD EXAMPLE (CAR CLASS)
CONCLUSION
• Main Points:
Public attributes and methods allow for flexibility in accessing
and modifying data.
Private attributes and methods help encapsulate and protect
sensitive data.
Using getter and setter methods maintains data integrity while
allowing controlled access.
END

004_Private and public attributes and methods.pptx

  • 1.
    PRIVATE AND PUBLIC ATTRIBUTESAND METHODS MARIO B. CADAY INSTRUCTOR I
  • 2.
    INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING(OOP) Main Points:  OOP is a programming paradigm based on the concept of objects. Objects represent real-world entities and are defined by attributes (data) and methods (functions).
  • 3.
    WHAT ARE ATTRIBUTESAND METHODS?  Attributes (Fields or Properties): Variables that hold data about the object.  Methods (Functions): Actions or behaviors that an object can perform.
  • 4.
    PUBLIC ATTRIBUTES ANDMETHODS • Definition: Public members are accessible from outside the class. • Use Case: Good for attributes or methods that need to be accessed by other classes or modules. • Syntax: Typically, no special symbol is required to declare something public.
  • 5.
    PRIVATE ATTRIBUTES ANDMETHODS  Definition: Private members are restricted to the class where they are defined.  Use Case: Used to hide implementation details and protect the integrity of data.  Syntax: In Python, prefix an attribute or method with two underscores __ to make it private.
  • 6.
    DIFFERENCES BETWEEN PUBLICAND PRIVATE MEMBERS  Access:  Public members can be accessed by any part of the code.  Private members can only be accessed within the class they are defined in. Example Comparison:  Public method can be called from outside: car.drive()  Private method can’t be accessed directly: car.__drive() would raise an error.
  • 7.
    WHY USE PRIVATEMEMBERS? Main Points: Encapsulation: Protects data by controlling access. Data Integrity: Prevents accidental modification. Abstraction: Hides the complexity of the internal workings of a class. Security: Secures sensitive information.
  • 8.
    GETTER AND SETTERMETHODS • Purpose: Provide controlled access to private attributes through public methods.
  • 9.
  • 10.
    CONCLUSION • Main Points: Publicattributes and methods allow for flexibility in accessing and modifying data. Private attributes and methods help encapsulate and protect sensitive data. Using getter and setter methods maintains data integrity while allowing controlled access.
  • 11.