Transcript
Page 1: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Java Objects and Classes

Page 2: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Overview

Creating objects that belong to the classes in the standard Java library

Creating your own classes

Page 3: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Definitions

Object-oriented Programming– involves 3 main concepts– Encapsulation– Inheritance– Polymorphism

Class vs Object (instance of class)

Page 4: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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

Page 5: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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);

Page 6: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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

Page 7: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Date deadline; //object variable

String s = deadline.toString(); //NO!

Need between

deadline =new Date();

or

deadline = birthday

Page 8: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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!

Page 9: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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

Page 10: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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

Page 11: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Building your own classes

A complete Java program requires one class with a main method.

No other classes have a main method

Page 12: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Simplest form

class NameOfClass

{

constructor1

constructor2

Methods

Fields

} //no ;

Page 13: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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);

Page 14: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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

Page 15: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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++

Page 16: Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

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;


Top Related