<< Chapter < Page Chapter >> Page >

Exercise It is possible to declare a nonabsract method in an abstract class. Give an example for this program, and explain why it is areasonable thing to do.

Equals

Concept There are two concepts of equality in Java: the operator == compares primitives types and references, while the method equals compares objects. The default implementation of equals is like == , but it can be overridden in any class.

Program: Inheritance07A.java

// Learning Object Inheritance07A //    equality (== vs. equals)class Particle {     int position;      Particle(int p) {        position = p;     }      void newPosition(int delta) {        position = position + delta;     }}  class AParticle extends Particle {     double spin;      AParticle(int p, double s) {        super(p);         spin = s;    }      void newPosition(int delta) {     if (spin < delta)             position = position + delta;    } }  class Inheritance07A {    public static void main(/*String[] args*/) {        AParticle a1 = new AParticle(20, 2.0);         AParticle a2 = a1;        AParticle a3 = new AParticle(20, 2.0);         boolean eqop12 = a1 == a2;        boolean eqop13 = a1 == a3;         boolean eqmethod = a1.equals(a3);    } }
  • Object a1 of type AParticle is created.
  • a1 is assigned to a2 using == .
  • Object a3 of type AParticle is created with the same values for its fields as the object referenced by a1 .
  • Evaluating a1==a2 returns true because they both reference the same object.
  • Evaluating a1==a3 returns false because they reference different objects.
  • Strangely enough, evaluating a1.equals(a3) returns false . Although their fields are equal, the default implementation of equals is the same as == !

Exercise Add the follow method to AParticle and run the program again. What happens now?

public boolean equals(AParticle a) {   return this.position == a.position && this.spin == a.spin; }

Program: Inheritance07B.java

// Learning Object Inheritance07B //    equality (overloading equals)class Particle {     int position;      Particle(int p) {        position = p;     }      void newPosition(int delta) {        position = position + delta;     }}  class BParticle extends Particle {     int charge;      BParticle(int p, int c) {        super(p);         charge = c;    }      public boolean equals(BParticle b) {         return  this.position == b.position &&                this.charge == b.charge;     }}  class CParticle extends BParticle {     boolean strange;      CParticle(int p, int c, boolean s) {        super(p, c);         strange = s;    }      void newPosition(int delta) {         if (strange)            position = position * charge;     }      public boolean equals(CParticle c) {        return    this.position == c.position &&                this.charge == c.charge &&                  this.strange == c.strange;     }}  class Inheritance07B {     public static void main(/*String[] args*/) {         BParticle b1 = new BParticle(20, 2);        BParticle b2 = new BParticle(20, 2);         CParticle c1 = new CParticle(20, 2, false);        CParticle c2 = new CParticle(20, 2, true);         boolean eqb1b2 = b1.equals(b2);        boolean eqc1c2 = c1.equals(c2);         boolean eqb1c1 = b1.equals(c1);        boolean eqc1b1 = c1.equals(b1);     }}

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask