Results 1 to 6 of 6

Thread: Generic methods to refactor code

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Generic methods to refactor code

    Hello,
    I have a little problem with the use of generic methods to refactor code. I actually trying to instantiate an object in a generic method as follows:
    Code:
    private <X extends A> X instanciationSelective(boolean Class) {
      X = anObject null;          
      if (Class)
        anObject = new Has();
      else
        anObject = new B();
      return anObject
    }
    with class B inherits class A. Obviously I'll give you a thousand, the compiler can not dip that anObject is an instance of A or its sub-classes and I released two best big error "incompatible types" (found A required X and B found required X ). If I cast upon instantiation, then I am entitled to the usual warning "unchecked cast". How to get out of there without error or warning ? Why in the methods generic substitution is not made (as in a generic class, any object type X has access to methods defined in A)?
    Last edited by Remedy; 12-01-2010 at 11:12 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Generic methods to refactor code

    Hello,
    The problem:
    1) You do not set X. Apart from knowing that extends A, we know nothing.
    2) nothing is known of X, and you try to assign an instance of A.
    3) nothing is known of X, we know nothing of B, and you try to assign an instance of A.
    The solution:
    Code:
    private A instanciationSelective(boolean Class) {
      A = anObject null;          
      if (Class)
        anObject = new Has();
      else
        anObject = new B();
      return anObject;
    }
    Yes, generics are not necessary in this case.

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Generic methods to refactor code

    Hello,
    It's normal that I do not define X as it is a generic.
    code of A and B in this example is very simple:
    class A ()
    class B extends A ()
    Yes, generics are not necessary in this case
    If still the problem with your solution is that the declared type of the instance returned will always be A and can not use the behavior of B without cast (the purpose of using generic types is precisely to avoid cast, thus avoiding any time to check the type of execution. Other ideas (if possible with generics unless another solution could be as clean)?

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Generic methods to refactor code

    Hello
    Thank you, I still know what a generic. The only possible solution then is to pass the class instead of a boolean as a parameter:
    Code:
    private <X extends A> X instanciationSelective (Class <X> class)
      throws InstantiationException, IllegalAccessException
    {
      return class.newInstance();
    }
    No feasible alternative, generic or not.

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: Generic methods to refactor code

    Hello,
    It's still amazing even when there is no alternative ... in addition, is con of using the class loader to do that because the compiler has even less control over the thing ... I feel it's going to the old caster. Yet it was a classic application of generic types. By the way thanks for your help. And if you have any alternative then please do post back.

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Generic methods to refactor code

    Hello,
    Yet it was a classic application of generic types ...
    No, your code is not at all type-safe.
    You have a statement like this:
    private <X extends A> X instanciationSelective(boolean Class)
    And code like this:
    B temp1 = instanciationSelective(true);
    B temp2 = instanciationSelective(false);
    The compiler has no way to check the return type of method because it depends on a boolean. It should be an analysis code to verify that this is not the compiler (and it might not necessarily do anyway). Explains a bit more what you do because it must surely be another solution. Also if you do the hard cast as using two different method (the sample instanciationSelective () that you show does not interest).

Similar Threads

  1. What are different methods of virtualization?
    By Deward in forum Networking & Security
    Replies: 6
    Last Post: 28-01-2011, 01:46 PM
  2. What is Abstract methods?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 11:59 AM
  3. Get and Set methods
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 02:20 PM
  4. Methods in JAVA
    By Caiden in forum Software Development
    Replies: 3
    Last Post: 18-08-2009, 11:26 PM
  5. Methods of debugging in PHP
    By Ninad23 in forum Tips & Tweaks
    Replies: 1
    Last Post: 21-11-2008, 02:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,248,577.32647 seconds with 17 queries