Java Objects and Classes
Overview
Creating objects that belong to the classes in the standard Java library
Creating your own classes
Definitions
Object-oriented Programming– involves 3 main concepts– Encapsulation– Inheritance– Polymorphism
Class vs Object (instance of class)
More definitions State - current set of values of the
object Methods - operate on objects; may
change state; state may affect behavior Inheritance in Java
– a class ‘extends’ another class– has all the properties and methods of class
extended and new methods and data fields that apply to new class
Using Existing Classes
Example: Math– only encapsulates methods, no data– do NOT construct a instance of class Math!– Call methods by using class name
Math.sqrt (x);
Using Existing Classes
Example: Date– construct them specifying initial state using
new, then apply methods
Date birthday = new Date();
Difference between object and object variable!!!– birthday refers to an object variable
Date deadline; //object variable
String s = deadline.toString(); //NO!
Need between
deadline =new Date();
or
deadline = birthday
Semantics
Date deadline = new Date(); Expression new Date() makes an object
of type Date and its value is a reference to that newly created object. The reference is then stored in the deadline object variable.
Can also set deadline = null;
but don’t call any methods for it!
Java and C++ Differences JAVA
Date birthday;
Date birthday = new Date();
Objects live on heaps, access bad reference, get error
Auto garbage collection
Clone to get a copy
C++
Date * birthday;
Date * birthday = new Date();
Access bad pointer, get another memory location
Destructors necessary Effort for copy and
assignment
Mutator and Accessor Methods
Mutator methods change state of object Accessor methods do NOT.
– In C++ should use const suffix for these, most of you probably don’t
– no distinction in Java Convention:
– mutators prefix method name with set– accessors use prefix get
Building your own classes
A complete Java program requires one class with a main method.
No other classes have a main method
Simplest form
class NameOfClass
{
constructor1
constructor2
Methods
Fields
} //no ;
Example: ComplexNumber.java
In main code:
ComplexNumber c = new ComplexNumber ();
ComplexNumber b = new ComplexNumber (4.5,5.5);
ComplexNumber [] cnums = new ComplexNumber[3];
cnums[0] = new ComplexNumber (5.4, 3.2);
cnums[1] = new ComplexNumber ();
cnums[2] = new ComplexNumber (3.0, 4.1);
Using Multiple Source Files
Put ComplexNumber class in ComplexNumber.java file
Put ManipCN class (main class) in ManipCN.java file
Make ComplexNumber class public! To compile:
javac ManipCN.java
Notes:
Constructor has same name as class classes can have more than one
constructor A constructor may take zero, one or
more parameters A constructor has no return value A constructor is called with the new
operator. //different from C++
Fields Final instance fields
– must be set before end of constructor and is never changed again
private final String name;
Static fields (class fields)– only one such field per class, shared among
all instances of the class
private static int nextId = 1;