Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


[JAVA] Upcasted Anonymous Classes: Can You Access Unique Fields/Methods?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 02 June 2013 - 09:13 AM

The book I'm reading doesn't make it clear, but I'm wondering if it's possible to access the unique fields and methods of an anonymous inner class that is upcasted to another. For example:

 

class Base{
  public int y = 10;

  public int returnStuff(){
   return y + 50;
  }//end of returnStuff

  public void printStuff(){
    returnStuff();
    System.out.println("The value: " + y );
  }//end of printStuff()

  public Base upcastAnonymous(){
    return new Base(){
       public int x = 5;
       {
         y = 30;
       }
    }//end of Anonymous Class
  }//end of upcastAnonymous

}//end of Base

public class EntryPoint{

  public static void main(){
    Base One = new Base();
    Base Two = One.upcastAnonymous();
    Two.printStuff();//I know this is gonna work, but...
    System.out.println(Two.x);//Is there any way to access Two.x here? I know this code won't compile.
  }//end of main()

}

 

I'd just like to know if there's some method of getting at Two.x that my book hasn't mentioned. I haven't found anything about it online. If you can't access the unique fields/methods once the class has been upcast, then are anonymous inner classes mostly for overloading a few methods without creating an entirely new class to do so?


Edited by Shaquil, 02 June 2013 - 09:20 AM.


Sponsor:

#2 rip-off   Moderators   -  Reputation: 8762

Like
2Likes
Like

Posted 02 June 2013 - 10:36 AM

Short of reflection, no. Also, don't use reflection =].

 

Anonymous inner classes are usually used to override the behaviour of one or more methods, most commonly single method interfaces such as Runnable, Callable and event handlers.



#3 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 02 June 2013 - 05:57 PM

Short of reflection, no. Also, don't use reflection =].

 

Anonymous inner classes are usually used to override the behaviour of one or more methods, most commonly single method interfaces such as Runnable, Callable and event handlers.

 

Thanks. I've never heard of reflection, so I'll have to look it up.



#4 dmatter   Crossbones+   -  Reputation: 3298

Like
0Likes
Like

Posted 03 June 2013 - 02:53 AM

are anonymous inner classes mostly for overloading a few methods without creating an entirely new class to do so?

Yes exactly, with "a few methods" usually being 1 or 2.
Typically they will just be implementing an interface too so all the behaviour is actually coming out of the anonymous class.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS