CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    90

    ..

    Hi all

    Just wondering if you're able to help me with my problem...

    This is not homework, just anonymised...



    I have an interface and a bunch of implementing classes:
    interface Fruit
    Banana implements Fruit
    Apple implements Fruit
    ....

    I have a Fruit utilities class. In this is a method that takes any sort Fruit and slices it.

    public static Fruit[] slice(Fruit f, int pieces)


    How can I declare the Fruit array to be of the same type as the Fruit I pass to the method?

    ie How can I automate Fruit[] a = new Apple[pieces] if I give it an Apple?
    Last edited by masher; September 2nd, 2009 at 06:51 PM.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to instantiate a class that implements an interface

    You can probably do that with a factory method - but I'm not sure I understand what you're trying to do... how can you slice a Fruit and get an array of Fruit as a result? Shouldn't it be an array of Slices? What methods will you call on the Fruit to get the specified number of slices?

    If you could pick an example that makes sense, it would be easier to help...

    Here's a (screwy) example:
    Code:
    Fruit[] slice(Fruit fruit, int pieces) {
        Fruit[] slices = new Fruit[pieces];  // array for the slices
    
        // fill the array
        for (int i = 0; i < slices.length; i++) {
            slices[i] = fruit.getSlice();  // get a new fruit of the same type
        }
    
        return slices;  // each will be a fruit of whatever type was passed in
    }
    
    void sliceFruit() {
        Apple apple = new Apple();
        Fruit[] slices = slice(apple, 4);  // returns an array of 4 Apples
        ...
    }
    
    interface Fruit {
        ...
        Fruit getSlice();  // factory method
    }
    
    class Apple implements Fruit {
        ...
        public Apple getSlice() {
            return new Apple();  
        }
    }
    
    class Banana implements Fruit {
        ...
        public Banana getSlice() {
            return new Banana();
        }
    }
    It's totally screwy because the example is nonsensical, but it works...

    The most important single aspect of software development is to be clear about what you are trying to build...
    B. Stroustrup
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: ..

    Quote Originally Posted by masher View Post
    How can I declare the Fruit array to be of the same type as the Fruit I pass to the method?

    ie How can I automate Fruit[] a = new Apple[pieces] if I give it an Apple?
    Even if Apple is a subtype of Fruit, an array of Apple is not a subtype of an array of Fruit.

    Sometimes you reach a limit with ordinary inheritance. You need a more powerful kind of polymorphism, namely generics. What you want to do is best accomplished with generics.

    This link isn't easy to learn from but it's the best reference there is,

    http://www.angelikalanger.com/Generi...nericsFAQ.html
    Last edited by nuzzle; September 7th, 2009 at 05:21 PM.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: ..

    Quote Originally Posted by nuzzle View Post
    You need a more powerful kind of polymorphism, namely generics.
    Angelika's Generics FAQ is an excellent reference, but it's worth remembering that generics in Java is not really a more powerful kind of polymorphism, it's very useful, but it's really just a compile-time type-checking mechanism. All traces of generic type arguments vanish during compilation (see type erasure), so there is no runtime representation of generic type arguments. As a result, decompiling Java code compiled from generic source gives you back a non-generic version. Unlike C++, it doesn't generate executable code for each template instantiation, which is a limitation of the Java implementation, even if it does avoid code-bloat.

    Bearing in mind that the static type information of generic parameters in Java is not available at runtime, so it doesn't allow generic creation of objects or arrays, I'm curious to know how you would use generics to resolve the OP's problem?

    In theory, there is no difference between theory and practice, but not in practice...
    Anon.
    Last edited by dlorde; September 7th, 2009 at 06:18 PM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: ..

    Quote Originally Posted by dlorde View Post
    ... it's worth remembering that generics in Java is not really a more powerful kind of polymorphism, it's very useful, but it's really just a compile-time type-checking mechanism. All traces of generic type arguments vanish during compilation
    You're making a very common error here. You're confusing abstraction with implementation.

    The important thing is how generics appears at the Java language level, not how it's currently implemented.

    Using generics, the OP will be sheltered from explicitly having to deal with very nasty inheritance constructs like downcasting. This will be taken care of behind the scenes in a typesafe manner by generics.

    I'm curious to know how you would use generics to resolve the OP's problem?
    You're looking for a freebie right?

    If you want to learn generics why don't you consider the Angelika Langer site I suggested and you recommended too.

    I think you should actually. Generics is a very powerful concept. It takes you beyond ordinary OO.
    Last edited by nuzzle; September 7th, 2009 at 07:44 PM.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: ..

    [QUOTE=nuzzle;1876397]
    You're looking for a freebie right?
    It's not my problem - your post implied you knew a solution using generics, and I was curious to see it. The OP might also be interested. If you don't want to share, or don't actually have a solution, so be it.

    That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted...
    G. Boole
    Last edited by dlorde; September 8th, 2009 at 05:53 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured