• 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

instanceof

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have three classes, 'Number', 'Integer' & 'Rational', as below:-





Now, if I do something like :-
Number n1 = new Integer("2");
Number n2 = new Rational("3/4");
Number result = n1.add(n2);

then, is there any way of doing this, without using the 'instanceof' command
in the Number class? I've read that using this is bad practice, but I can't see
any other way around it.

[Edit - added code tags - see UseCodeTags for details]
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this particular case I would make Integer a subclass of Rational (an integer IS-A rational number), and then get rid of the concept of Number entirely. Which makes the problem go away until you decide to incorporate irrational numbers into your application.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it's ever a good design for superclasses to have to know about their subclasses. Which means that there shouldn't be any references to Integer or Rational in your Number class.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, rename Integer to something that doesn't conflict with a core Java class name.
 
Jeremy Watts
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul, I have good reason for the inclusion of the 'Number' class. The main one being that, I never know in advance what any particular arithmetic method is going to return. For example, two Integers added/subtracted/multipled will always produce an Integer, but not so Rationals. Two Rationals added may produce an Integer, for example. So, what do you use as a return type in your methods in these cases? Well, you'd need something abstract, hence 'Number'. The situation gets more complicated with the addition of other number types, such as Complex numbers, Gaussian Integers etc

There are other reasons too. Such as using 'Number' to include methods that implement algorithms that apply to all number types.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about making your classes implement a RationalNumber interface? You ought not call it Number, for the same reason you ought not to use the name Integer.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Watts wrote:@Paul, I have good reason for the inclusion of the 'Number' class. The main one being that, I never know in advance what any particular arithmetic method is going to return. For example, two Integers added/subtracted/multipled will always produce an Integer, but not so Rationals. Two Rationals added may produce an Integer, for example.



I really don't think it's a good idea to have 1/2 + 1/4 return a Rational, and 1/2 + 1/2 return a number of a completely different type! That's going to get very messy. I would expect a Rational added to a Rational to always return a Rational, and since integers are rational numbers that shouldn't be a problem.
 
Jeremy Watts
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell, Yes thanks, an interface worked well! And thanks everyone else who replied
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
reply
    Bookmark Topic Watch Topic
  • New Topic