Forum Stats

  • 3,853,778 Users
  • 2,264,267 Discussions
  • 7,905,444 Comments

Discussions

dealing with functions not in class Object

793927
793927 Member Posts: 61
edited Sep 10, 2002 11:14AM in Generics
Hi,
while i was working on GJ i faced the following problem, when i write a generic class i have to take into my consideration those functions that i want to define for the row type class and not included in class Object (toSting() is in class Object but print() is not). of course every time i want to call print() from the generic class the compiler gives me an error becuz it is not in class Object.. is there anyway to get rid of this problem in a simple way without having to deal with more than one row type ?
thanks everyone

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    It is not at all clear to me what you're trying to do. Please post 10 or 20 lines of code which illustrate your problem.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    I think I see what you're getting at...the GJ compiler substitues Object for all unconstrained type parameters, and therefore tries to call methods on parametized type objects as if they were simply of type Object.

    A solution would be to create a base class (or interface) AbstractRow that declares all the methods you need. Then constrain your parameters against AbstractRow. Eg:

    public interface AbstractRow
    {
    public AbstractRow rowMethod();
    }

    public class MyClass<RowType extends AbstractRow>
    {
    public RowType doSomething(RowType rt) {
    rt.rowMethod();
    }
    }

    I think that should work. I'm not sure what you meant by "without having to deal with more than one row type." If you meant "without having to introduce an inheritance hierarchy," then no, this is not possible in GJ (as far as I can tell). If you meant "without having to write adapters for every single row type," then yes.

    shaun
This discussion has been closed.