Gaming Quick Java Question

  • Thread starter Thread starter mayhem366
  • Start date Start date
  • Views Views 921
  • Replies Replies 4

mayhem366

Well-Known Member
Member
Joined
Nov 24, 2008
Messages
181
Reaction score
0
Trophies
1
Location
UK
XP
84
Country
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.
 
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?
smile.gif


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
 
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.
 
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()
smile.gif

You usually set it to EXIT_ON_CLOSE
 
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.
 

Site & Scene News

Popular threads in this forum