- Forward


An Introduction to Classes
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • What is a set?
    • A finite or infinite collection of unique elements/members (in which order may or may not have significance)
    • Expand
  • How are sets defined?
    • Extension - Listing all of the elements/members
      (e.g., \(S = \{1, 3, 5\}\))
    • Intension - Describing the properties of the elements/members
      (e.g., \(T = \{t: 0 \leq t \leq 10\}\))
    • Expand
Classes
Back SMYC Forward
  • Definition:
    • A definition of a set that is written in terms of the properties of the elements (i.e., an intensive definition of a set)
    • The set of elements so defined
  • What is an object?
    • An element/member of the set
  • Encapsulation:
    • (v) The process of defining an entity in terms of its characteristics
    • (n) A result of the encapsulation process
Timing of this Discussion
Back SMYC Forward
  • An Observation:
    • Java is a pure object-oriented language so all code must be in a class
  • The Implication:
    • We have to have some understanding of classes before we can write any code
Typical Programming/Software Engineering Sequences
Back SMYC Forward
  • Getting Started:
    • Students are given engineering designs and must implement them
  • Somewhere in the Middle:
    • Students are only expected to go from a textual description of a class to a final encapsulation (using a refinement process)
  • By the End:
    • Students are expected to understand and use several software processes to go from needs and desires to deployment
Typical Programming/Software Engineering Sequences (cont.)
Back SMYC Forward
  • Implications:
    • In this course you won't be doing much design -- you'll be given a design that you must implement
  • Nonetheless:
    • You still need to know something about the way classes are designed
The Design and Implementation of Classes
Back SMYC Forward
  • What is involved in designing and implementing a class?
    • Creating an intensive definition of the members (i.e., a list of attributes and behaviors)
  • What is involved in designing and implementing the attributes?
    • Declaring the names and types of instance attributes (i.e., properties of the instances) and class attributes (i.e., properties of the class)
  • What is involved in designing and implementing the behaviors?
    • Declaring the signatures (i.e., the name, return type, and formal parameters) of instance and class methods
    • Implementing the bodies of instance and class methods
    • Declaring and implementing constructors (i.e., special methods that are used to initialize the instance attributes of objects)
Using Parameters in Methods
Back SMYC Forward
  • An Observation:
    • Methods often need to be given information they need, this information is said to be passed to the method as parameters
  • Terminology:
    • The parameters in the signature of the method (i.e., where the information is received) are called formal parameters
    • The parameters in the invocation of the method (i.e., where the information is provided) are called actual parameters or arguments
Information Hiding
Back SMYC Forward
  • The Principle:
    • Hide the internal details of a component from all other components
  • The Rationale:
    • Prevents damage from errant external code
    • Makes components easier to understand/use
    • Simplifies modification and repair
    • Facilitates re-use
Information Hiding Guidelines
Back SMYC Forward
  • Instance Attributes:
    1. Declare all instance attributes private
    2. Provide public getter methods (that allow external code to "see" the contents of attributes) where needed
    3. Provide public setter methods (that allow external code to "determine" the contents of attributes) where needed
  • Class Attributes:
    1. Declare class attributes that are only used inside the class private (and final if appropriate)
    2. Declare class attributes that are used outside of the class public and final
  • A Concept We'll Return To:
    • Immutability - Classes with instance attributes that can't be changed after they are initialized (e.g., because they only have corresponding getters) are said to define immutable objects
Naming Conventions in Java
Back SMYC Forward
  • Class Names:
    • Begin with an uppercase letter
  • Primitive/Fundamental/Atomic Types:
    • Begin with a lowercase letter
  • Variables/Attributes/Parameters:
    • Begin with a lowercase letter (except for "constants" which are in all uppercase)
  • Method Names:
    • Begin with a lowercase letter
Simplified Syntax of a Class
Back SMYC Forward

access class class-name
{

[
access [static] type attribute [, attribute]... ;
]...


[
access class-name([type param [, type param]...])
{
}
]


[
access [static] type method([type param [, type param]...])
{
}
]...
}

Simplified Syntax (cont.)
Back SMYC Forward
An Example
javaexamples/oopbasics/pf_classes/Purchase.java (Fragment: 0)
 
Interchangeable Terms
Back SMYC Forward
  • Member:
    • Characteristic
  • Attribute:
    • Field
    • Property
  • Method:
    • Behavior
    • Operation
A Potentially Confusing Situation
Back SMYC Forward
  • The Confusion:
    • Formal parameters often have the same name/identifiers as the attributes of the class
  • Resolving the Confusion:
    • this. can be used to disambiguate attributes and formal parameters (and local variables) with the same name
A Potentially Confusing Situation (cont.)
Back SMYC Forward
An Example of Disambiguation
javaexamples/oopbasics/pf_classes/Purchase.java (Fragment: Disambiguation)
 
Java vs. Python - Important Differences
Back SMYC Forward
  • Constructors:
    • In Python, initialization is performed in the __init__() method
  • Accessibility Modifiers:
    • In Python, all members are public by default (but starting the name with _ is a strong suggestion to not access it from outside)
Using Variables in Methods
Back SMYC Forward
  • An Observation:
    • A method may need to perform intermediate calculations
  • A Solution:
    • Use local variables (i.e., variables that can only be used within the method) in addition to the attributes
Using Variables in Methods (cont.)
Back SMYC Forward
  • Local Variables (and Parameters):
    • Can only be used in the method that declares them
  • Private Attributes:
    • Can be used in all methods in the class
Class Members
Back SMYC Forward
  • Class Attributes:
    • Sometimes a class itself has attributes and/or all of the objects in a class have the same value for one or more attributes
  • Class Methods:
    • Sometimes an object's behavior does not depend on its attributes (in which case, it is as if the behavior belongs to the class)
Class Members (cont.)
Back SMYC Forward
An Example of Class Attributes
javaexamples/oopbasics/pf_classes/Purchase.java (Fragment: StaticAttributes)
 
Class Members (cont.)
Back SMYC Forward
  • Common Uses of Class Attributes:
    • Class constants, flags and special values
    • Common values (across all instances)
    • Object counters
  • Common Uses of Class Methods:
    • Type conversion
    • "Factory" methods
    • Attribute-free calculations
Utility Classes
Back SMYC Forward
  • Definition:
    • A class that contains only static attributes and methods
  • Purpose:
    • Provide the kind of functionality provided by functions in Python
The Main Class
Back SMYC Forward
  • Purpose:
    • Provides an entry point to the program/application (i.e., the first statement to execute)
  • Defined:
    • A class with a public static void method named main() that is passed a String[]
  • Understanding Why:
    • main() is static so that an object doesn't have to be created in order to invoke it
    • main() is passed a String[] so that the user can provide information (called command-line arguments or run argumens) to the program at start-up
The Main Class (cont.)
Back SMYC Forward
  • An Observation:
    • A "traditional" class can be the main class if it includes a main() method with the approrpiate signatue
  • Good Practice for Beginning Programmers:
    • Have a "special" class for this purpose (i.e., a class with only a main() and, perhaps, private static helper methods)
There's Always More to Learn
Back -