Gaming Quick Java Question

mayhem366

Well-Known Member
OP
Member
Joined
Nov 24, 2008
Messages
181
Trophies
0
Location
UK
XP
74
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.
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,099
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?
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
 

mayhem366

Well-Known Member
OP
Member
Joined
Nov 24, 2008
Messages
181
Trophies
0
Location
UK
XP
74
Country
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.
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,099
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()
smile.gif

You usually set it to EXIT_ON_CLOSE
 

mayhem366

Well-Known Member
OP
Member
Joined
Nov 24, 2008
Messages
181
Trophies
0
Location
UK
XP
74
Country
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

General chit-chat
Help Users
  • BakerMan
    The snack that smiles back, Ballsack!
    BakerMan @ BakerMan: well, after a day of pain, i'm gonna catch some zs