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

Download Report

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

Computer Science I
Classes and Objects
Professor: Evan Korth
New York University
Evan Korth
New York University
Road Map
•
•
•
•
•
•
•
Introduction to object oriented programming.
Classes
Encapsulation
Members
Objects
Constructors
Reading:
– Liang 7: chapter 7: 7.1 – 7.4
– Liang 8: chapter 8: 8.1 – 8.5
Evan Korth
New York University
Object Oriented Programming
• Emphasis is placed on nouns or objects.
• Nouns (objects) have properties and
behaviors.
• How do we build these objects?
• How do we represent their properties?
• How do we define their behaviors?
Evan Korth
New York University
Classes
• The main building blocks of Java programs.
• Defines objects of the same type. Like a
blueprint.
Evan Korth
New York University
Classes (cont)
• Every .java file has one or more classes. Only one
of the classes can be a public class.
– That class must have the same name as the .java file.
• If the class has an method called main(), execution
can begin in that class. (Therefore, you can test a
class by adding a main method to it.)
• If there are other classes in the file, they cannot be
public classes.
Evan Korth
New York University
Encapsulation
• Encapsulation refers to the process of
combining elements to create a new entity.
• You encapsulate the properties (attributes)
and behaviors (activities) of an entity into a
class.
• Encapsulation also enables us to hide the
implementation of a class to other classes
(information hiding / abstraction).
Evan Korth
New York University
Designing Classes
• A class declaration includes members of the
class.
• A member can be either a data member or a
method member.
• A data member (AKA field) is used to
define state (attributes or properties) of the
entity.
• A method member is used to define the
behaviors of the entity.
Evan Korth
New York University
Data members
• Data members can be a primitive type or a
reference to another object*.
– Primitive types are integer types, floating point
types, characters and booleans. (Note: an int is
not the same as an object of type Integer)
• The scope of a data member is the entire
class, no matter where within the class it is
declared.
* More on object references in a moment
Evan Korth
New York University
Default values for data members
• 0 for all numeric type variables (including
both floating point types and all integer
types)
• \u0000 for char variables
• null for reference variables*
• false for boolean type variables
• Note: No default values for local variables
(variables declared inside a method).
* More on object references in a moment
Evan Korth
New York University
Objects
• An object is an instance of a class.
• If we think of a class as a blueprint, an
object is one model created from that
blueprint.
• You can create any number of objects from
one class.
• An object is distinctly identified by an
object reference (except for anonymous
objects).
Evan Korth
New York University
Declaring object references
• In order to reference an object, we need an
object reference variable.
• To declare an object reference variable we
use the syntax:
ClassName objectReferenceName;
• The above statement creates a variable
objectReferenceName which can
reference a ClassName object. It does
NOT create an object.
Evan Korth
New York University
Instantiating objects
• In order to create an object, we use the new
keyword along with a constructor* for the class of
the object we wish to create.
• To refer to the object, we “point” an object
reference variable to the new object.
objectReferenceName = new Constructor();
• The declaration and instantiation can be combined
as follows:
ClassName objectReferenceName = new ClassName();
– Note: the name of a constructor is the same as the name of the class
* More on constructors soon
Evan Korth
New York University
Accessing Members of a Class
• Within a class you can access a member of the
class the same way you would any other variable
or method.
• Outside the class, a class member is accessed by
using the syntax:
– Referencing variables:
objectReferenceName.varName
– Calling methods (sending messages):
objectReferenceName.methodName(params)
Evan Korth
New York University
Constructors
• Constructors are special methods that
instantiate objects.
• A constructor is invoked with the new
operator.
• A constructor should initialize the class
variables. If the variables are not
initialized, default values are used.
• A constructor does not have a return type.
• A constructor’s identifier (name) is the
same as the class itEvan
constructs.
Korth
New York University
Constructors continued
• Constructors can be overloaded but each one must
have its own signature.
• A constructor with fewer arguments can call a
constructor with more arguments (we will see how
to do this soon).
• If no constructor is defined, a default constructor
is automatically supplied which accepts no
parameters. Variables are initialized to their
default values.
• If one constructor is explicitly defined, the
automatic default constructor is no longer
available. In such case, if you want a no
parameter constructor, you must define it yourself.
Evan Korth
New York University