java objects and classes. overview n creating objects that belong to the classes in the standard...

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




4 download

Embed Size (px)


  • Java Objects and Classes

  • OverviewCreating objects that belong to the classes in the standard Java libraryCreating your own classes

  • DefinitionsObject-oriented Programminginvolves 3 main conceptsEncapsulationInheritancePolymorphismClass vs Object (instance of class)

  • 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

  • Using Existing ClassesExample: Mathonly encapsulates methods, no datado NOT construct a instance of class Math!Call methods by using class nameMath.sqrt (x);

  • Using Existing ClassesExample: Dateconstruct 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 variableString s = deadline.toString(); //NO!

    Need betweendeadline =new Date();or deadline = birthday

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

  • Java and C++ Differences JAVADate birthday;Date birthday = new Date();

    Objects live on heaps, access bad reference, get errorAuto garbage collectionClone to get a copy C++Date * birthday;Date * birthday = new Date();

    Access bad pointer, get another memory locationDestructors necessaryEffort for copy and assignment

  • 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

  • Building your own classesA complete Java program requires one class with a main method.No other classes have a main method

  • Simplest formclass NameOfClass{ constructor1 constructor2MethodsFields}//no ;

  • Example: ComplexNumber.javaIn 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 FilesPut ComplexNumber class in filePut ManipCN class (main class) in fileMake ComplexNumber class public!To compile:javac

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

  • FieldsFinal instance fields must be set before end of constructor and is never changed againprivate final String name;

    Static fields (class fields)only one such field per class, shared among all instances of the classprivate static int nextId = 1;


View more >