1 / 12

Java – Methods Part II Lecture Notes 6

Java – Methods Part II Lecture Notes 6. Objects, Classes and Computer Memory. When a Java program is executing, the memory must hold: 1. Templates for all classes used 2. Variables that refer to objects 3. Objects Memory for methods allocated within classes

zarek
Télécharger la présentation

Java – Methods Part II Lecture Notes 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java – Methods Part IILecture Notes 6

  2. Objects, Classes and Computer Memory When a Java program is executing, the memory must hold: 1. Templates for all classes used 2. Variables that refer to objects 3. Objects Memory for methods allocated within classes Memory for data allocated within objects

  3. Characteristics of Objects An object has a A Behavior (methods) A State an identity Objects identity is handled by Java Virtual Machine (JVM) When the object is out of the scope, then the “Garbage Collector” purges the object from the memory Eg: { Account myAccount; ….. { Account yourAccount; } }

  4. Designing Methods First Understand the user requirments What kind of public behaviors do they want? Eg: Elevator class Methods needed: Constructors: 1. A default constructor public Elevator() { currentFloor=0; direction=up;} A Specific Constructor public Elevator(int floor, direction dir){ currentFloor = floor; direction = dir; }

  5. Designing Methods Some Accessors: public final int floor() { return currentFloor; } Some Mutators: public void moveToNextFloor() { currentFloor++; }

  6. Designing Methods public void move(){ // pseudo code // if elevator is moving up check the next // service requested floor above and move there by invoking move // else check to see if anyone below has requested // service } * Complete the code assuming elevator has a boolean array of floors: bool [] floorList;

  7. SubClasses and SuperClasses public class Elevator { ….. } Suppose that Super Elevator is a subclass of elevator public class SuperElevator extends Elevator { public SuperElevator(){ super(); // calls the super class constructor nonStop = true; } What is super?

  8. SubClasses and SuperClasses What is super? Super activates methods from the super (or parent class) eg: super() - activates the default constrcutor super(argument list) - activates a specific constructor Activating other methods super.<method name> ( <arguments list>) eg: super.moveToFloor(n); where moveToFloor is a method in elevator class.

  9. What is this? public Elevator (int currentFloor) { this.currentFloor = currentFloor; } The word “this” is a reserved word. “this” refers to the object itself. This.currentFloor refers to whatever the current objects instance Eg: Elevator SouthElevator(5); Elevator NorthElevator(4);

  10. “this” is good to check the equality Suppose you want to see two objects are the same. Suppose that Elevator class has a method called compareTo public boolean compareTo(Elevator E){ if (this.id == E.id) return true; else return false; } //so in the main myElevator.compareTo(yourElevator);

  11. Testing for equality There are two ways to check for equality using == operator using instance method “equals” - method defined in the object class and uses the == operator by default Eg: System.out.println(x==1); System.out.println(x.equals(1)); How about System.out.println(str==“guna”); // if str = “guna” System.out.println(str.equals(“guna”));

  12. Testing for equality The operator == compares the reference to an object,not the content The method: object.equals(otherObject) checks the equality of content

More Related