Results 1 to 6 of 6
  1. #1
    tawalaya is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Question generics and inheritance

    Hi, I was searching a bit within the forums but couldn't quit find what I was looking for, hopefully you guys can help me anyway :)

    For a school project I am required to create a Class that handles fraction numbers. Which is in it self not that complicated and I got it working pretty quickly. Anyway what I wonted to do now, was to create a abstract super class called Number that would require any inheritor to have a few methods like: add, sub, div and so on.

    So here is what i did:

    Java Code:
    public abstract class Number extends java.lang.Number {    
        public abstract  <T extends Number> T add(T a,T b);
        public abstract <T extends Number> T sub(T a,T b);
    }
    The problem however it that I wonted the inheritors to look like this:
    Java Code:
    public class Fraction extends Number {
        
        public Fraction add(Fraction a, Fraction b) ...
        
        public Fraction mult(int value, Fraction f) ...
    }
    but instead i have to write like this to get correct code:

    Java Code:
    public class Fraction extends Number {
        @Override
        public <T extends Number> T add(T a, T b) ...
        @Override
        public <T extends Number> T sub(T a, T b) ...
    }
    Is the result I wont possible? I tired various methods so far without any luck. Thanks for your help ;)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that you want to declare your generics on the class definition line:

    Java Code:
    abstract class MyNumber<T extends Number> {
       public abstract T add(T t1, T t2);
       public abstract T sub(T t1, T t2);
    }

    I doubt that you want this class to truly extend Number, but if so, please let me know your thinking as to why.

    Also, it's very confusing to name your class Number and also use java.lang.Number in your code.

  3. #3
    tawalaya is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    First of all thanks for your quick response ;)

    Regarding my poor choice for a name I have to agree with you, for the moment you can use this class body instead of the one from my first post:

    Java Code:
    public abstract class MyNumber...
    to inherit the methods of java.lang.Number is actually not needed jet. My main problem was that I wonted to have each inheritor of MyNumber to define methods like add only on them self like this.
    For my fractal Numbers:
    Java Code:
    public Fraction add(Fraction a, Fraction b)...
    For some other inheritor:
    Java Code:
    public Inheritor add(Inheritor a, Inheritor b)
    I hope you get where I am going with this, but as far as i read on the web there is not really a way to do this with Java?

  4. #4
    tawalaya is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok i think i found a solution to my problem:

    the MyNumber class:
    Java Code:
    public abstract class MyNumber<T extends MyNumber> { 
        
        public abstract   T add(T a,T b);
    }
    the Fraction class:
    Java Code:
    public class Fraction extends com.autoex.model.math.Number<Fraction> {
    
        @Override
        public Fraction add(Fraction a, Fraction b) ...
    
    }
    Is this a good way to do it or is there a better way to do it?

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The prinicple looks OK, but shouldn't the Fraction class extend the MyNumber class instead of com.autoex.model.math.Number?

    The MyNumber class doesn't seem to have any data or method implementations, so you could consider making it an interface.

  6. #6
    tawalaya is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    You are completely right ;) it should have been com.autoex.model.math.MyNumber ;) I did forgot to chage it after renaming my Number class :P and i guess making it an interface makes even more sence ;) thanks for the tip

Similar Threads

  1. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 06:04 AM
  2. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 08:58 PM
  3. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM
  4. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-11-2008, 12:08 AM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 10:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •