aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extracting hierarchy of objects from Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extracting hierarchy of objects from Vector" Watch "Extracting hierarchy of objects from Vector" New topic
Author

Extracting hierarchy of objects from Vector

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
I am elaborating on a Whizlabs example. Superclass shape, subclasses circle square & rhombus. One instance of each subclass, each has a string name attribute.

I wish to output in a loop two columns ie


How can I access the name of each vector element in a loop, do I need to cast them into object?



Thank you!


Nigel
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello Nigel,

Please try to change the for loop this way


You can do certain other modifications as well, that override the toString() method in each class and simply print out the object like
this



Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

I have slightly modified the code more, and have a look at it.


Hope this helps,
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 532
    
    7
Hi,

I suggest an improvement: Push up the definitions of name, getName() and setName() into the superclass Q2_Vector_Shapes, instead of duplicating them in each subclass.

Benefits:
> Code reuse

> Avoids code duplication

> Avoids type based checks. Instead of code like

you can utilize polymorphism like this:

The advantage is that if you introduce a new shape subclass like Triangle in future, you won't have to change this for-loop.

Cheers
Karthik
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

I thought the same, but he doesn't have a name definition inside the shapes class. I thought he want the
modification, but sticking to same line of action.

Cheers,
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Hi Prithvi,

Very helpful, that works really well. It seems one has to write a lot of code, to copy it back into a new object.

toString makes such a difference:
I haven't overridden toString before, even if reduced to:
it works, without it, it does not.

Are you able to explain how the toString works, I guess it is overriding the version in the Vector class?

Thank you
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

toString() is a method defined in the Object class. Basically, every class inherits a toString() method.
Basically toString() method is used, if you want to know something meaningful about the object you are using.
If you don't override a version present in the the Object class
and do something like this


It will basically print the class name and some hexadecimal value with it. So this kind of object representation is not
really meaningful. Now if you override the toString method, something like this


Now in the above mentioned statement, it will print out the vechicle name + vehicle number.
Basically it is more meaningful information then the previous snippet of code. Very helpful
in debugging and spitting some state about your object.

Hope this helps,

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Nigel Shrin wrote:Hi Prithvi,

I guess it is overriding the version in the Vector class?

Thank you


No nigel, it is overriding the version from the Object class. Think, you are not extending from Vector class.



HTH,
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Thank you for your great replies Prithvi and Karthik, I now have it working very neatly using your suggestions.

Superclass:


Subclass:


Thanks again
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Good work Nigel. Exactly right approach and demonstrate polymorphism as well.

Good luck,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting hierarchy of objects from Vector