Making Objects
Last modified Sunday, 07-Jul-2024 16:06:39 UTC.
- Objects
- The Big Picture
(Slides)
- A class is instantiable if we can create instances of the class.
- An object is an instance of a class.
- An object contains
- An object is created with the new operator, and is referred to by an object
variable (a reference variable).
- The new operator specifies the name of the class the instance is to be
made from.
- The data and methods in an object are the instance data and methods in the class.
- Parameters may be given to the class name in a new statement, which are
normally used to give initial values to the data in the object through a
constructor method ... more details coming soon.
- An object's data and methods are accessed using the syntax
<object variable>.<data or method name>
- Example: CurrencyConverter1.java
and UseCurrencyConverter1.java
- Create a project named something appropriate
- Create the two classes in the src directory
- Select the class containing the main method to run
- It is possible to have multiple classes with a main method in one project
- It is possible to do it all in one file, if a class is very specific to a program
- Example:
CurrencyConverter1OneFile.java
- Notice the absence of a visibility modifier
- The class can be used in only that file
- Seperate files for now ...
- Many objects can be created from the same class.
- Constructors
- Imagine what happens without the call to randConverter.setExchangeRate in
UseCurrencyConverter1.java
- A constructor is a special method that is automatically executed when a new instance of
the class is created.
- The purpose of the constructor is to initialize (the data members of) an object to
a valid state.
- The name of a constructor must be the same as the name of the class.
- Constructors have no return type (not even void)
- For every class you should define a default constructor that does something (useful)
- Non-default contructors can be added (typically) to provide initial values for variables.
(A class can include multiple constructors, by using overloading.)
- Constructors may obtain initial information from the user
- Methods in Objects
- Objects that IO themselves
- A well encapsulated class has no public instance variables
- It is common for an object to have a toString method.
println method calls toString when printing an object.
- It is common for an object to have a method that inputs values for its instance
variables.
- Example: Species1.java and
UseSpecies1.java
- Accessor and Mutators
- A well encapsulated class has no public instance variables
- It is therefore necessary to provide
- Constructors to initialize them
- Accessor methods to get their values
- Mutator methods to change their values
- Output methods are examples of accessors
- Input methods are examples of mutators
- Mutators can check for validity of data values
- Example: Species2.java and
UseSpecies2.java
- Helper Methods
- Helper methods "help" the public methods complete their tasks.
- Helper methods that are used only within objects are private.
- Example: Species3.java and
UseSpecies3.java
Exercises
- Do the static or non-static parts of a class define what's in objects that are instances of
the class?
- What operator is used to create an object?
- What happens if you don't supply a default constructor method?
Exam Style Questions
- Write a class suitable for storing information about the governor of a state.
It must store the name, the fraction of the vote attained, and whether or he (s)he is
honest (not a constant :-).
Provide no methods except a default constructor.
- When is a constructor method of a class automatically called?
- In the class MyBigFatGreekWedding, what is the name of the constructor method?
Does it have a return type?
- What is the benefit of having a toString method in a class?
- What are mutator and accessor methods?