Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Abstract classes, and 'instanceOf'

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Abstract classes, and 'instanceOf'

    Ok, I have 3 classes, An abstract one (called 'NumberType'), and two standard ones (called 'Real' & 'Complex'). The two standard classes are child classes of the abstract class.

    As you may have guessed, these things do basic arithmetic on real & complex numbers. Inside each, are overloaded methods handling all combinations of the basic operations - such as a 'real added to a real', a 'real added to a complex', a 'complex added to a real', a 'complex added to a complex' and so on..

    And then, inside the NumberType class, I have abstract methods such as:-

    abstract NumberType add(Real number);
    abstract NumberType add(Complex number);

    The reason that the return type here is 'NumberType', (and also the entire point of this class's existence anyway), is that upfront, there would be no way of knowing what type of number would be returned by this method. For instance, two complex numbers added, may in some instances, return a real number. And similar for other combinations.

    Now, I also have included a non-abstract method in the NumberType class, that handles
    a NumberType added to a NumberType. It goes like this:-
        public NumberType add(NumberType number)
        {
            if (number instanceof Real == true)
            {
                return(this.add((Real)number));
            }
            else
            {
                if (number instanceof Complex == true)
                {
                    return(this.add((Complex)number));
                }
            }
        }
    Now, finally, the point of my entire question. Is there anyway of doing this without having to resort to using the rather frowned-upon 'instanceOf' statement? I have tried a few things, without success, such as generics. But cannot get anything to work, without using instanceOf.

    Thanks
    Last edited by helloworld922; August 11th, 2012 at 11:50 AM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Abstract classes, and 'instanceOf'

    Why doing this?The method add is overloaded,so when called with real or complex number,at compilation time the compiler will select the right one.You do not need to check it by yourself.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Abstract classes, and 'instanceOf'

    Sure, yes.. but what I neglected to mention in my original post, was something like this:-

    Lets say, that I am initially adding two numbers of the Complex class, then because I will not know what type of number will be returned here straight off (for example, two complex numbers added may return another complex, or possibly a real), then my 'return type' for the addition call, has to be of NumerType type. For example:-

    Complex c1 = new Complex(2, 3);
    Complex c2 = new Complex(4, -3);

    NumberType result = c1.add(c2);

    (in this example, then a real is actually returned)

    Now, what if i want to use this 'result' in a further calculation? Such as:-

    Real t = new Real(5.0);

    NumberType newResult = t.add(result);

    Here then, I must include code to handle the case of a 'Real' being added to a 'NumberType', which is the code in the NumberType class, containing the 'instanceOf' statements.. which are the very thing I wish to avoid using.
    Last edited by jezza10181; August 11th, 2012 at 05:54 AM.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Abstract classes, and 'instanceOf'

    So i you not implement an add with NumberType as argument then you probably get a compiler error.You could cast inside this add function to real and then check for a null exception.If this exception is not thrown then it is ok,so you call the right add method.Then you could do the same for complex.But this is more inefficient than the instance of style you posted before(in my opinion)

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Abstract classes, and 'instanceOf'

    Why not have every number be complex? By definition a real number is just a complex number with a zero imaginary part.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Abstract classes, and 'instanceOf'

    Sure, yes, that is possible, and is something that I originally thought of doing. But there are soliid reasons why I want to do it this way. One is that of computational efficiency, treating reals as separate to complex's allows a more efficient multiplication algorithms, for instance. This may sound slightly trivial, but if you're carrying out very many of these computations, then the efficiency improvement is noticeable.

    The second reason is that the subject of maths (or math, if you're American) itself makes clear distinction between number types (it has different set notations for each, R (for reals), C (for complex numbers), Z (for integers) etc etc. and i wish to make use of these distinctions further down the line.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Abstract classes, and 'instanceOf'

    I think this is an case of trying to prematurely optimize your code.

    Handling all potentially complex numbers as purely complex isn't that slow. It's definitely not slow compared to the checks required to determine if the result should be real or complex or which method to call. There just isn't a way to know this at compile time so any solution will require runtime computation. This is further complicated by memory and SIMD optimizations (which I'm not sure to what extend Java can take advantage of).

    It also adds unnecessary code bulk which makes code maintenance more difficult.

    If you know a computation will always be real, I would suggest you stick with the primitive doubles and floats. Otherwise, you will have to incur the penalty of using complex numbers.

Similar Threads

  1. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 02:34 PM
  2. [SOLVED] abstract classes, can you explain?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 08:01 PM
  3. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  4. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM