• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using interface as a type

 
Hama Kamal
Ranch Hand
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys ,, i got it
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Achilleas Achix
Ranch Hand
Posts: 123
BSD Java Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 123
BSD Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
please check below code and answer my questions at the bottom of the peace of code if you can

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic