5.1 Anatomy of a Class
Enduring Understanding
Programmers use code to represent a physical object or nonphysical concept, real or imagined, by defining a class based on the attributes and/or behaviors of the object or concept.
Learning Objective
Designate access and visibility constraints to classes, data, constructors, and methods.
Essential Knowledge
The keywords public and private affect the access of classes, data, constructors, and methods.
The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
Classes are designated public.
Access to attributes should be kept internal to the class. Therefore, instance variables are designated as private.
Constructors are designated public.
Access to behaviors can be internal or external to the class. Therefore, methods can be designated as either public or private.
Essential Understanding
When multiple classes contain common attributes and behaviors, programmers create a new class containing the shared attributes and behaviors forming a hierarchy. Modifications made at the highest level of the hierarchy apply to the subclasses.
Learning Objective
Designate private visibility of instance variables to encapsulate the attributes of an object.
Essential Knowledge
Data encapsulation is a technique in which the implementation details of a class are kept hidden from the user.
When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.
Instance variables are encapsulated by using the private access modifier.
The provided accessor and mutator methods in a class allow client code to use and modify data.
Last updated
Was this helpful?