• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extracting hierarchy of objects from Vector

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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


Hope this helps,
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good work Nigel. Exactly right approach and demonstrate polymorphism as well.

Good luck,
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic