1 / 10

Object Methods

Object Methods. Things To Remember. Private. Private variables may only be used by the class in which they are declared. No client program may ever access a private instance variable. Class Using Its Own P.I.V. public class Ball { private int diameter; private String color;

Download Presentation

Object Methods

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. Object Methods Things To Remember

  2. Private • Private variables may only be used by the class in which they are declared. • No client program may ever access a private instance variable.

  3. Class Using Its Own P.I.V. • public class Ball { private int diameter; private String color; public void makeSameSize(Ball otherBall) { diameter = otherBall.diameter; } • This is a legal use of the P.I.V. since we are still in class Ball when we write .diameter • Note: there would also be constructors and other methods in this class.

  4. Incorrect Use Of P.I.V. • public class BallVendor { public String sellBigBall(Ball myBall) { if(myBall.diameter > 10) return “Ball sold.” ; } } • This is an illegal use of a P.I.V. since this is not class Ball to which diameter belongs, but a separate class designed to use the Ball class a.k.a a client program.

  5. this • Sometimes in the context of a class method we must refer to the object itself. • A classic example is the compareTo( ) method. • The convention for compareTo( ) is to return a negative if the object that called the method is less than or before the parameter object, a positive for the opposite, and a 0 if they are the same.

  6. Using compareTo( ) • Let’s assume class BallVendor has: • Ball ball1 = new Ball(5); // diameter = 5 Ball ball2 = new Ball(6); // diameter = 6 int comp = ball1.compareTo(ball2); • comp should be a negative since ball1 is smaller

  7. How It Works • The compareTo( ) method would then exist in class Ball • public intcompareTo(Ball that) { if(this.getDiameter( ) < that.getDiameter( ) ) return -1; else if(this.getDiameter( ) > that.getDiameter( ) ) return 1; else return 0; }

  8. Constructors • It is the job of the constructor to assign a value to all private instance variables. • A default(empty) constructor gives the P.I.V. a default value: public Ball( ) { diameter = 0; } // 0 is good default for int } • All other constructors set the P.I.V. to the parameter: public Ball( int aDiameter) { diameter = aDiameter; } // P.I.V. = parameter

  9. References v. Fundamental Type • MAJOR CAVEAT!! • If in a class: intx = 5; Ball b = new Ball(5); change(x,b); public void change(intone, Ball two) { one += 1; two.add1ToDiameter( ); } System.out.print(x + “ “ + b); • Assuming there were toString( ) and add1ToDiameter( ) methods in Ball, the result would print: 5 6 !!!!! • This is because one got x’s value, but when one changed, x did not. However two was told to point at the same object as b. When two called a method that changed the value of that object, b began to point to the changed object!

  10. toString( ) • Every object class should have a toString( ) method. • JOPane and System.out automatically call this method on any variable. • This is why: int x = 10; System.out.print(x); would print the String version of the int 10 • Assuming Ball has a toString( ) that displays the diameter, Ball aBall = new Ball(10); System.out.print(aBall); • Would print 10.

More Related