Java Objects and Classes
Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes
Definitions n Object-oriented Programming –involves 3 main concepts –Encapsulation –Inheritance –Polymorphism n Class vs Object (instance of class)
More definitions n State - current set of values of the object n Methods - operate on objects; may change state; state may affect behavior n 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 n 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 n Example: Date –construct them specifying initial state using new, then apply methods Date birthday = new Date(); n 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(); n 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(); n Objects live on heaps, access bad reference, get error n Auto garbage collection n Clone to get a copy C++ Date * birthday; Date * birthday = new Date(); n Access bad pointer, get another memory location n Destructors necessary n Effort for copy and assignment
Mutator and Accessor Methods n Mutator methods change state of object n Accessor methods do NOT. –In C++ should use const suffix for these, most of you probably don’t –no distinction in Java n Convention: –mutators prefix method name with set –accessors use prefix get
Building your own classes n A complete Java program requires one class with a main method. n No other classes have a main method
Simplest form class NameOfClass { constructor1 constructor2 Methods Fields }//no ;
Example: ComplexNumber.java n 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 n Put ComplexNumber class in ComplexNumber.java file n Put ManipCN class (main class) in ManipCN.java file n Make ComplexNumber class public! n To compile: javac ManipCN.java
Notes: n Constructor has same name as class n classes can have more than one constructor n A constructor may take zero, one or more parameters n A constructor has no return value n A constructor is called with the new operator. //different from C++
Fields n Final instance fields –must be set before end of constructor and is never changed again private final String name; n Static fields (class fields) –only one such field per class, shared among all instances of the class private static int nextId = 1;