Computer Science I Classes and Objects Professor: Sana Odeh New York University Evan Korth New York University.

Download Report

Transcript Computer Science I Classes and Objects Professor: Sana Odeh New York University Evan Korth New York University.

Computer Science I
Classes and Objects
Professor: Sana Odeh
New York University
Evan Korth
New York University
Road Map
• Dealing with multiple files
• modifiers
• Static variables
Reading
– Liang 4: chapter 6: 6.5 – 6.8
– Liang 5: chapter 6: 6.6, 6.7, 6.10, 6.11
– Liang 6: chapter 7.5 – 7.8, 7.11, 7.14
Evan Korth
New York University
review
• What is meant by the term encapsulation?
• What are the default values for data
members?
• What does it mean to instantiate an object?
• What does this statement do?
Integer i;
Evan Korth
New York University
Review
• Given:
Integer i;
What does the following statement do?
i = new Integer(100);
• Generally, what should a constructor do?
Evan Korth
New York University
Modifiers
• Java provides us with several keywords used to
modify the accessibility of variables, methods and
classes.
– Visibility modifiers
• public
• private
• protected
• (None)
– others
• static
• final
• abstract
Evan Korth
New York University
Principle of least privilege
• You should pick the modifier that allows the
least privilege for other classes while
allowing your code to do what it needs to
do.
• This helps reduce debugging time by
localizing potential problem areas.
Evan Korth
New York University
Data member modifiers
• No modifier (default) means the data is visible in the
package in which it is declared.
• public means the data is visible to everything.
• private means the data is visible only within the
class in which it is defined.
– Trying to access private data from another class will result in
a compile time error.
• final means the variable cannot be changed.
• There are two other modifiers applicable to variables:
– static : We will discuss in a moment
– protected: We will discuss later in the semester.
Evan Korth
New York University
Accessor methods
• When a data member is declared to be
private, we still need a way to refer to that
data. A method used to change or retrieve a
private data item is referred to as an
accessor method.
• Two kinds of accessor methods are the get
method and the set method.
Evan Korth
New York University
Get methods
• A method that is used to retrieve the value
of a data object is referred to as a get
method.
• Also known as a getter.
• Get method header should look like this:
public returnType getPropertyName ()
• It may just return a data field or it may
calculate the value. Remember information
hiding.
Evan Korth
New York University
Predicate methods
• A get method that returns a Boolean value
should have a header like this:
public boolean isProperty ()
• It can simply return a Boolean data field or
it can use a Boolean formula to calculate it’s
data. Remember, information hiding!
Evan Korth
New York University
Set methods
• Methods used to set or change the value of a
data method are referred to as set methods.
• Also known as setters and mutators.
• Header of set method will look like this:
public void setProp (propType)
Evan Korth
New York University
Data modifiers (cont)
• A data member can be either an instance
variable or a static variable (also known as a
class variable).
Evan Korth
New York University
Static variable (AKA class variable)
• A static variable has only one value no matter how
many objects are instantiated from a class. The value is
shared by all instances of the class.
• A static variable does not need an instance of the
class in order to be accessed.
• You can access a static variable either with
ClassName.varName (better style), or
objectReference.varName notation.
• For static variables, every object of the class refers to the
same memory location.
• Static variables can be accessed by static methods OR
instance methods.
• The memory for a static Evan
variable
is allocated when the
Korth
New York University
class is loaded into memory.
Instance variables
• An instance variable has a unique value for each
object of that class.
– This does not mean that two objects cannot have the
same value; it does mean that those values will be
stored separately in memory.
• You can access an instance variable only with
objectReference.varName notation.
• No memory is allocated until an object is
instantiated.
• Can be accessed by instance methods only (we
will talk about instance methods in just a
moment). (i.e. not by static methods)
Evan Korth
New York University
Scope of data members
• Whether a data member is a class variable
or an instance variable, it’s scope is the
entire class. It does not matter where in the
class, the variable is declared.
• Remember, if they are not initialized, data
members are assigned a default value.
Evan Korth
New York University
Local method variables
• Do not automatically get initialized.
– Using them without initializing them is a
compilation error.
• Cannot have visibility modifiers.
• The scope of a local method variable starts
where it is declared. It ends at the end of
the block where it was declared.
Evan Korth
New York University
Method members
• Methods are used to define the behaviors of
an object.
• They can be overloaded.
– Having more than one method in a class with
the same name is referred to as method
overloading.
– Each of the methods must have a different
method signature. That is, they must have
different argument lists.
Evan Korth
New York University
Method modifiers
• No modifier means the method is visible in the package
in which it is declared.
• public means the method is visible to everything.
• private means the method is visible only within the
class in which it is defined.
– Trying to call a private method from another class will result
in a compile time error.
• static means it is a static method. Static methods can
use other modifiers as well.
• There are three others (final, protected and
abstract) which we will discuss later in the semester.
• There are still others which we will not discuss this
Evan Korth
semester.
New York University
Static methods (AKA class methods)
• Can be called without an instance of the
method.
• All the methods in the Math class are static
methods which is why we can call them
without a Math object. In fact, we cannot
instantiate an object of the Math class.
• You can call a static method either with
ClassName.method (args) (better style),
or objectReference.method (args)
notation.
Evan Korth
New York University
Instance methods
• Can only be called after an object is
instantiated.
• You can call an instance method only with the
objectReference.method (args)
notation.
• An instance method acts on the specific
instance for which it has been called.
Evan Korth
New York University