Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes."— Presentation transcript:

1 Java Objects and Classes

2 Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

3 Definitions n Object-oriented Programming –involves 3 main concepts –Encapsulation –Inheritance –Polymorphism n Class vs Object (instance of class)

4 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

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

6 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

7 Date deadline; //object variable String s = deadline.toString(); //NO! Need between deadline =new Date(); or deadline = birthday

8 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!

9 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

10 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

11 Building your own classes n A complete Java program requires one class with a main method. n No other classes have a main method

12 Simplest form class NameOfClass { constructor1 constructor2 Methods Fields }//no ;

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

14 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

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

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


Download ppt "Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes."

Similar presentations


Ads by Google