This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using interface as a type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using interface as a type " Watch "Using interface as a type " New topic
Author

Using interface as a type

Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

hey guys,,, please check the problem bleow
1-is it a correct way to call the method in this way?
??
2-when i run it it gives this error
createobjectdemo.Rectangle@c17164

we have 3 classes and one interface


The point class



the Rectangle class



The Relatable Interface


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hama Kamal wrote:
hey guys,,, please check the problem bleow
1-is it a correct way to call the method in this way?
??

yes if that method has a return type.

Hama Kamal wrote:
2-when i run it it gives this error
createobjectdemo.Rectangle@c17164

this is not an error. it is just default string representation of your Rectangle object. if you want the representation more meaningful, then override toString method in Rectangle class.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Seetharaman Venkatasamy wrote:
Hama Kamal wrote:
hey guys,,, please check the problem bleow
1-is it a correct way to call the method in this way?
??

yes if that method has a return type.

Hama Kamal wrote:
2-when i run it it gives this error
createobjectdemo.Rectangle@c17164

this is not an error. it is just default string representation of your Rectangle object. if you want the representation more meaningful, then override toString method in Rectangle class.


thanks alot for your reply ,,, but according to bleow method it shuold return either rectOne or rectTwo! but it does not



i mean the output is like this now:
Width of rectOne: 100
Heigh of rectOne: 200
Area of rectOne: 20000
X Position of rectTwo: 23
Y position of rectTwo: 94
X positon of rectTwo: 40
Y positon of rectTwo:72
1
createobjectdemo.Rectangle@c17164 but it should be either rectOne or rectTwo am i right?
BUILD SUCCESSFUL (total time: 1 second)
pauly bradley
Greenhorn

Joined: May 17, 2011
Posts: 8
Hi Hama,

the problem is that you are returning something of type 'Object'. And you print the 'Object' which invokes the default 'toString' method. Hence you get the default string Seetharaman mentions. If you want to print the contents of rectOne or rectTwo you need to cast the return.


cheers.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Thank you guys ,, i got it
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hama Kamal wrote:Thank you guys ,, i got it


Hello Hama Kamal, hello Ranchers...@Hama could you be kind enough to show us how you overrode the toString() method? I am unable -at the moment- to put together an 'effective' override of the toString() method in such a way that it will return 'rectOne' OR 'rectTwo' according to the return of the findLargest() method in line 43 of the main class:



I know how to override the toString() method, but how can I 'condition' the output of the toString() method in such a way that it will display 'rectOne' OR 'rectTwo'


I will appreciate your help and that of any other coleague.

Regards

Ikpefua.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

Ikpefua, Rectangle has only height, width and point. You have to use ONLY those in your toString() method.
Why would you want to employ more than 1 Rectangle instances?
IMOH here:


- the cast Rectangle otherRect = (Rectangle)other; is not needed.
- public Object findLargest(Object object1, Object object2) could very well be static
- public Object findLargest(Object object1, Object object2) could very well have a signature of findLargest(Rectangle object1, Rectangle object2) , no point in having arguments of type Object.


OCPJP 6.0
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Achilleas...Thanks man that was the idea I had all along and thats what I did:



Now I got confused when @Hama said;
but it should be either rectOne or rectTwo
So I was wondering if there was a way 'rectOne' or 'rectTwo' could display as output.
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

Ikpefua Jacob-Obinyan wrote:@Achilleas...Thanks man that was the idea I had all along and thats what I did:



Now I got confused when @Hama said;
but it should be either rectOne or rectTwo
So I was wondering if there was a way 'rectOne' or 'rectTwo' could display as output.


rectone is the name of the variable. There is no way in java for an Object to know the name of the variable which points to (refers to) it!!!

MAN!!! You just gave a GREAT idea!!! An Object to know the name and type of the variable it is pointed to. Or to be more specific a list of all variables which point to it!!
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Achilleas wrote:MAN!!! You just gave a GREAT idea!!! An Object to know the name and type of the variable it is pointed to. Or to be more specific a list of all variables which point to it!!


Achilleas please permit me laugh first... , the idea started from the rancher that started this thread 'Hama Kamal', we could 'propose' that to the new onwers of java 'Oracle'..."We want objects to identify variables that refer to them"
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Hi all
please check below code and answer my questions at the bottom of the peace of code if you can

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

println has a number of overloaded versions. One of them takes an Object as the argument, and when that one is called it uses toString() to work out what to display.

If you check the source code, you'll see this in PrintStream (the actual type of System.out):

And in String you'll find this:

So toString() gets called, even though you never call it yourself.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Matthew Brown wrote:println has a number of overloaded versions. One of them takes an Object as the argument, and when that one is called it uses toString() to work out what to display.

If you check the source code, you'll see this in PrintStream (the actual type of System.out):

And in String you'll find this:

So toString() gets called, even though you never call it yourself.


thank you very much Matthew Brown ,,,, your explanation is perfect and helpful..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using interface as a type