6 Replies - 237 Views - Last Post: 03 October 2012 - 09:09 AM Rate Topic: -----

#1 bmckinn8  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-September 11

Checking to see if objects are of the same class

Posted 03 October 2012 - 07:59 AM

I have a parent class, 'Alpha', that has a method 'matches' that checks to see if objects are of the same class. Then I also have a child class, 'Beta', that inherits the 'matches' method. My problem is that in the Beta matches method I need to add a clause that compares the 'rank' of both objects after they have been checked for class equality. Hopefully the code below will help illustrate my problem further.

class Alpha
public class Alpha 
{
	//matches method
	public boolean matches(Alpha other)
	{
		if (this.getClass() == other.getClass())
		{	
			//System.out.println(this.getClass() + " " + other.getClass());
			return true;
		}
		return false;
	}
}



class Beta
public class Beta extends Alpha
{
	protected int rank;
	
	//constructor
	public Beta(int rank)
	{
		this.rank = rank;
	}

	//matches method
	public boolean matches(Alpha other)
	{
		super.matches(other);
		//if(rank == other.rank)
			return true;
		//return false;
	}
}



class Test
class Test
{
	
	public static void main(String[] args)
	{
		Alpha c0 = new Alpha();
		Alpha c1 = new Alpha();
		Alpha b2 = new Beta(1);
		Alpha b3 = new Beta(2);
		
		/***
		 * check to see if c0 and c1 are of the same class
		 *
		 * should return 'PASS'
		 ***/
		
		if (c0.matches(c1))
			System.out.println("matches:				PASS");
		else
			System.out.println("matches:				FAIL");
		
		/***
 		 * check to see if c0 and c1 are of different classes	
		 * 
		 * should return 'FAIL'
		 ***/
		
		if (!c0.matches(c1))
			System.out.println("matches:				PASS");
		else
			System.out.println("matches:				FAIL");
		
		/***
		 * check to see if c1 and b2 are of the same class
		 *
		 * should return 'FAIL'
		 ***/
		
		if (c1.matches(b2))
			System.out.println("matches:				PASS");
		else
			System.out.println("matches:				FAIL");	
		
		/***
		 * check to see if b2 and b3 are of the same class AND have the same rank
		 * 
		 * should return 'FAIL'
		 ***/
	
		if (b2.matches(b3))
			System.out.println("matches:				PASS");
		else
			System.out.println("matches:				FAIL");	
		}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Checking to see if objects are of the same class

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 08:11 AM

Are you looking for something like this:

public boolean matches(Alpha other)
{
   if(!(other instanceof Beta)) return super.matches(other);

   Beta b = (Beta)other;
   if(rank == b.rank) return true;

   return false;
}

Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 08:32 AM

I would have used instanceof just like Kakerergodt did

Instead of having a matches method, I would override the equal method
//Alpha class
@Override
public boolean equals(Object other) {
	return other instanceof this;
}

//Beta class
@Override
public boolean equals(Object other) {
	if (super.equals(other))
		return other.rank == this.rank;
	return false;
}



That way you are able to compare two instances using the == operator :)
Beta beta1 = new Beta();
Beta beta2 = new Beta();
System.out.println(beta1 == beta2);



Edit: Wrote Other instead of Object

This post has been edited by CasiOo: 03 October 2012 - 08:33 AM

Was This Post Helpful? 1
  • +
  • -

#4 bmckinn8  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-September 11

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 08:32 AM

That is exactly it! I don't know why I couldn't figure that out, the UML specified it as a 'matches' method, but approaching just like an overridden 'equals' method makes perfect sense.

This post has been edited by bmckinn8: 03 October 2012 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 08:37 AM

Ops I also forgot the cast to a Beta object :P

//Beta class
@Override
public boolean equals(Object other) {
	if (super.equals(other))
		return ((Beta) other).rank == this.rank;
	return false;
}




Was This Post Helpful? 0
  • +
  • -

#6 xriBit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-October 12

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 09:06 AM

To add on the above solutions. Here's what I can say.

Study on programming principles and you'll realize that "Abstraction is the key".

Also, be careful with the Casting. You might get troubles with IS-A.
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: Checking to see if objects are of the same class

Posted 03 October 2012 - 09:09 AM

View PostxriBit, on 03 October 2012 - 04:06 PM, said:

To add on the above solutions. Here's what I can say.

Study on programming principles and you'll realize that "Abstraction is the key".

Also, be careful with the Casting. You might get troubles with IS-A.


IS-A ? explain
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1