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

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

Post on 03-Jan-2016

219 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Java Objects and Classes</p></li><li><p>OverviewCreating objects that belong to the classes in the standard Java libraryCreating your own classes</p></li><li><p>DefinitionsObject-oriented Programminginvolves 3 main conceptsEncapsulationInheritancePolymorphismClass vs Object (instance of class)</p></li><li><p>More definitionsState - current set of values of the objectMethods - operate on objects; may change state; state may affect behaviorInheritance in Java a class extends another classhas all the properties and methods of class extended and new methods and data fields that apply to new class</p></li><li><p>Using Existing ClassesExample: Mathonly encapsulates methods, no datado NOT construct a instance of class Math!Call methods by using class nameMath.sqrt (x);</p></li><li><p>Using Existing ClassesExample: Dateconstruct them specifying initial state using new, then apply methods Date birthday = new Date();</p><p>Difference between object and object variable!!!birthday refers to an object variable</p></li><li><p>Date deadline; //object variableString s = deadline.toString(); //NO!</p><p>Need betweendeadline =new Date();or deadline = birthday</p></li><li><p>SemanticsDate 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 dont call any methods for it!</p></li><li><p>Java and C++ Differences JAVADate birthday;Date birthday = new Date();</p><p>Objects live on heaps, access bad reference, get errorAuto garbage collectionClone to get a copy C++Date * birthday;Date * birthday = new Date();</p><p>Access bad pointer, get another memory locationDestructors necessaryEffort for copy and assignment</p></li><li><p>Mutator and Accessor MethodsMutator methods change state of objectAccessor methods do NOT.In C++ should use const suffix for these, most of you probably dontno distinction in JavaConvention:mutators prefix method name with setaccessors use prefix get</p></li><li><p>Building your own classesA complete Java program requires one class with a main method.No other classes have a main method</p></li><li><p>Simplest formclass NameOfClass{ constructor1 constructor2MethodsFields}//no ;</p></li><li><p>Example: ComplexNumber.javaIn main code:</p><p>ComplexNumber c = new ComplexNumber ();ComplexNumber b = new ComplexNumber (4.5,5.5);</p><p>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);</p></li><li><p>Using Multiple Source FilesPut ComplexNumber class in ComplexNumber.java filePut ManipCN class (main class) in ManipCN.java fileMake ComplexNumber class public!To compile:javac ManipCN.java</p></li><li><p>Notes:Constructor has same name as classclasses can have more than one constructorA constructor may take zero, one or more parametersA constructor has no return valueA constructor is called with the new operator. //different from C++</p></li><li><p>FieldsFinal instance fields must be set before end of constructor and is never changed againprivate final String name;</p><p>Static fields (class fields)only one such field per class, shared among all instances of the classprivate static int nextId = 1;</p></li></ul>