views:

114

answers:

4

Sorry for the crappy title I failed to think of a better version for my Java question. I am right now using Java version: 1.6.0_18 and Netbeans version: 6.8

Now for the question.

What I've done is created a class with only one protected int property and next I made a public method to Set the int property to a given value. Then I made an object of that class and used said public method to set the int property to 5. Now I need your help to create another class that will take said object and expose it's protected int property.

The way I could think of doing this was to create a sub class to inherit said class and then create a method to Get the int property of the super class. I kind of succeeded to create the code to Get the int property but now I can't figure out how to use this new sub class to reference the object of the super class.

Here are the 2 classes I have thus far:

public class A
{
  protected int iNumber;

  public void setNumber ( int aNumber )
  {
    iNumber = aNumber;
  }
}
public class B extends A
{
  public int getNumber()
  {
   return super.iNumber;
  }
}

I created an object of 'A' and used its method to set its property to 5, like this:

A objA = new A();
objA.setNumber ( 5 );

Now I want to create an object of 'B' to output the int stored within the property of 'objA'. I've tried to run this code:

B objB = (B) objA;
String aNumber_String = String.valueOf( objB.getNumber() );
System.out.println( aNumber_String );

but I got the error: "java.lang.ClassCastException" on the first line

 B objB = (B) objA; 

Please is there anyway of doing what I am trying to do?

P.S. I am hoping to make this idea work because I do not want to edit class A (unless I have no choice) by giving it a getter method.

P.P.S Also I know it's a 'bad' idea to expose the property instead of making it private and use public setter / getter methods but I like it this way :).

Edit: Added code tags

+1  A: 

You can't cast a A object to B. If you want to call getNumber, you must have a real B object:

B objB = new B();
objB.setNumber ( 5 );
System.out.println( objB.getNumber() );

No casts are necessary.

Matthew Flaschen
I was afraid of that, I guess objects of class A would be useless now. Thankyou :)
+1  A: 

For the line

B objB = (B) objA;

the object of class A is not a object of class B, so that cast would not be allowed.

The class relationship between A and B is, that B is-a A, (because class B extends A), but the inverse cannot be said in this case.

Take the following for example, where the following exists:

  • class Animal.
  • class Dog extends Animal

What is being attempted in the above example is to cast an Animal to a Dog:

Dog dogObject = (Dog)animalObject;  // Not allowed.

This cannot be necessarily the case, as not all Animals are Dogs -- for all we know, the animalObject could be an instance of the class Cat which extends Animal, which is definitely not a Dog.

coobird
I like the analogy, I understand why it wont work now, ah well. Thankyou :)
A: 

As others have said, you can't cast an A object to a B because of their relationship. What I'd add is that it really sounds like you only have one class, but you have some situations in which you want some other objects not to be able to read the value of the number. I'd say this is better served with an interface. Define the interface to only have the setNumber(int) method. Define the class to have both the getNumber() and the setNumber(int) methods, and to implement your "setter" interface. Then, wherever you have a method that accepts an A object as a parameter, change it to accept your interface.

This will allow you to have code that is not allowed to get the value of the object, but simplifies your code by only having one class defined.

Chris Shaffer
I like that idea, I may end up using it actually. Thankyou :)
A: 

Do you mind telling us why you don't want to modify class A and why you don't like the private variables with getters/setters?

Ben Rose
I have the rest of my program working exactly the way I want and I just know everything will break if I start fiddling around with class A. Also, the values I will be storing have already been verified / validated so there is no need for getter / setter methods to double check.
May I suggest a source code repository (such as subversion) then? That way, if you fiddle the wrong way and break everything, you'll have a copy backed up that you can revert to. Good luck!
Ben Rose