Quick Java Question

Discussion in 'Computer Games and General Discussion' started by mayhem366, Feb 13, 2010.

Feb 13, 2010

Quick Java Question by mayhem366 at 2:56 PM (535 Views / 0 Likes) 4 replies

  1. mayhem366
    OP

    Member mayhem366 GBAtemp Regular

    Joined:
    Nov 24, 2008
    Messages:
    181
    Location:
    UK
    Country:
    United Kingdom
    Is it possible to override some methods of a class as you instantiate it?
    i.e. something like this perhaps:
    Code:
    SomeClass sc = new SomeClass() {
    public void someMethodToOverride() {doSomeStuff;}
    }
    No idea if i have just made this up but I think i have seen this somewhere before but have never used it myself and don't particularly want to create a new class just to override one method.
     
  2. tj_cool

    Supervisor tj_cool Stuff

    Joined:
    Jan 7, 2009
    Messages:
    9,942
    Location:
    This planet
    Country:
    Belgium
    You can create a class to overwrite one single method, but you'd have to instantiate an object from that class somewhere else, so you can probably do it in that place too...
    Depends on what you need it for. Maybe you can give more info about what you want and what you're overriding? [​IMG]

    Possible is:
    Code:
    public class Someclass {
    ÂÂÂÂ@Override
    ÂÂÂÂpublic void mymethod() {
    ÂÂÂÂÂÂÂÂdo this;
    ÂÂÂÂÂÂÂÂ}
    ÂÂÂÂ}
    
    --------------------------------------------
    
    public class otherclass {
    ÂÂÂÂSomeclass sc = new Someclass();
    ÂÂÂÂsc.mymethod();
    }
    Or you can work with inner classes:
    Code:
    public class Someclass() {
    ÂÂÂÂprivate class Innerclass() {
    ÂÂÂÂÂÂÂÂpublic void mymethod() {
    ÂÂÂÂÂÂÂÂÂÂÂÂdo this;
    ÂÂÂÂÂÂÂÂ}
    ÂÂÂÂ}
    }
    You still have to do a blah blah = new Innerclass(); though
     
  3. mayhem366
    OP

    Member mayhem366 GBAtemp Regular

    Joined:
    Nov 24, 2008
    Messages:
    181
    Location:
    UK
    Country:
    United Kingdom
    Yeah that is still creating a whole new class just for one overridden method, all i wanted to do was edit how a JFrame closed, can't remember off the top of my head how i would go about doing that, some WindowListener i'd imagine.
    I think i'll go with the inner class method as this seems more what i had in mind.

    Thank you for your help.
     
  4. tj_cool

    Supervisor tj_cool Stuff

    Joined:
    Jan 7, 2009
    Messages:
    9,942
    Location:
    This planet
    Country:
    Belgium
    What do you mean with How it closes?
    If you mean what it does when you click the "X" button, you can change that with setDefaultCloseOperation() [​IMG]
    You usually set it to EXIT_ON_CLOSE
     
  5. mayhem366
    OP

    Member mayhem366 GBAtemp Regular

    Joined:
    Nov 24, 2008
    Messages:
    181
    Location:
    UK
    Country:
    United Kingdom
    Yeah yeah i know that, that is what i have at current. I need it to disconnect a wiimote before the system closes and maybe some other stuff also.
     

Share This Page