Implement an interface and override methods in Java?
Why do you have to override all methods of an interface?
For instance if I have
public class Foo extend JFrame implements ActionListener, KeyListener
foo()
@Override
public void keyPressed(KeyEvent arg)
@Override
public void keyReleased(KeyEvent arg)
@Override
public void keyTyped(KeyEvent arg)
I'm going to have lots of methods I won't even be using, is there a way to remove the un used implemented methods, for instance if i plan to use one method from the interface
I don't want to use abstract either, as this means I can't create an instance of the object (at least my compiler says so)
java interface implements
add a comment |
Why do you have to override all methods of an interface?
For instance if I have
public class Foo extend JFrame implements ActionListener, KeyListener
foo()
@Override
public void keyPressed(KeyEvent arg)
@Override
public void keyReleased(KeyEvent arg)
@Override
public void keyTyped(KeyEvent arg)
I'm going to have lots of methods I won't even be using, is there a way to remove the un used implemented methods, for instance if i plan to use one method from the interface
I don't want to use abstract either, as this means I can't create an instance of the object (at least my compiler says so)
java interface implements
add a comment |
Why do you have to override all methods of an interface?
For instance if I have
public class Foo extend JFrame implements ActionListener, KeyListener
foo()
@Override
public void keyPressed(KeyEvent arg)
@Override
public void keyReleased(KeyEvent arg)
@Override
public void keyTyped(KeyEvent arg)
I'm going to have lots of methods I won't even be using, is there a way to remove the un used implemented methods, for instance if i plan to use one method from the interface
I don't want to use abstract either, as this means I can't create an instance of the object (at least my compiler says so)
java interface implements
Why do you have to override all methods of an interface?
For instance if I have
public class Foo extend JFrame implements ActionListener, KeyListener
foo()
@Override
public void keyPressed(KeyEvent arg)
@Override
public void keyReleased(KeyEvent arg)
@Override
public void keyTyped(KeyEvent arg)
I'm going to have lots of methods I won't even be using, is there a way to remove the un used implemented methods, for instance if i plan to use one method from the interface
I don't want to use abstract either, as this means I can't create an instance of the object (at least my compiler says so)
java interface implements
java interface implements
edited Nov 14 '18 at 1:47
jww
52.9k39226495
52.9k39226495
asked Apr 13 '11 at 10:38
AlanFosterAlanFoster
5,86943048
5,86943048
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:
addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
// handle typed key here
);
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extendsKeyAdapter. Then you'd only have to write the one method you want to override.
– WhiteFang34
Apr 13 '11 at 10:47
add a comment |
Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?
add a comment |
You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.
When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.
add a comment |
This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):
public class Foo extend JFrame
foo()
...
component.addKeyListener(new MyKeyListener());
...
private class MyKeyListener extends KeyAdapter
@Override
public void keyPressed(KeyEvent arg)
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
add a comment |
If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?
However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.
Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.
add a comment |
You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5647800%2fimplement-an-interface-and-override-methods-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:
addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
// handle typed key here
);
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extendsKeyAdapter. Then you'd only have to write the one method you want to override.
– WhiteFang34
Apr 13 '11 at 10:47
add a comment |
Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:
addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
// handle typed key here
);
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extendsKeyAdapter. Then you'd only have to write the one method you want to override.
– WhiteFang34
Apr 13 '11 at 10:47
add a comment |
Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:
addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
// handle typed key here
);
Concrete classes must always implement all of the methods of an interface. If you weren't already extending JFrame you could extend KeyAdapter. It implements empty methods for KeyListener to avoid writing them out. You could use an anonymous class with that inside of your Foo class like this:
addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
// handle typed key here
);
edited Apr 13 '11 at 10:50
answered Apr 13 '11 at 10:40
WhiteFang34WhiteFang34
58.5k1689106
58.5k1689106
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extendsKeyAdapter. Then you'd only have to write the one method you want to override.
– WhiteFang34
Apr 13 '11 at 10:47
add a comment |
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extendsKeyAdapter. Then you'd only have to write the one method you want to override.
– WhiteFang34
Apr 13 '11 at 10:47
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
I can only extend one class though?
– AlanFoster
Apr 13 '11 at 10:45
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extends
KeyAdapter. Then you'd only have to write the one method you want to override.– WhiteFang34
Apr 13 '11 at 10:47
Yeah you can only extend one class. You'd have to split your key logic into a separate class that extends
KeyAdapter. Then you'd only have to write the one method you want to override.– WhiteFang34
Apr 13 '11 at 10:47
add a comment |
Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?
add a comment |
Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?
add a comment |
Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?
Interfaces have no default implementation. If you weren't forced to implement each method in the interface, then what would happen when some other code tries to call these methods through the interface?
answered Apr 13 '11 at 10:41
Graham BorlandGraham Borland
48.6k16118162
48.6k16118162
add a comment |
add a comment |
You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.
When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.
add a comment |
You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.
When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.
add a comment |
You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.
When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.
You can make a parent class of Foo which has these empty methods and have Foo only implement the methods you want to see.
When you implement an interface, you must implement all its methods somewhere for a concrete object to be created.
answered Apr 13 '11 at 10:42
Peter LawreyPeter Lawrey
443k56564966
443k56564966
add a comment |
add a comment |
This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):
public class Foo extend JFrame
foo()
...
component.addKeyListener(new MyKeyListener());
...
private class MyKeyListener extends KeyAdapter
@Override
public void keyPressed(KeyEvent arg)
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
add a comment |
This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):
public class Foo extend JFrame
foo()
...
component.addKeyListener(new MyKeyListener());
...
private class MyKeyListener extends KeyAdapter
@Override
public void keyPressed(KeyEvent arg)
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
add a comment |
This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):
public class Foo extend JFrame
foo()
...
component.addKeyListener(new MyKeyListener());
...
private class MyKeyListener extends KeyAdapter
@Override
public void keyPressed(KeyEvent arg)
This is exactly what the KeyAdapter class is for: it implements all methods of the interface (but none of the implementations do anything). Since you cannot extend two classes, you should use an inner class for your listener (which is a cleaner design anyway):
public class Foo extend JFrame
foo()
...
component.addKeyListener(new MyKeyListener());
...
private class MyKeyListener extends KeyAdapter
@Override
public void keyPressed(KeyEvent arg)
answered Apr 13 '11 at 10:43
Michael BorgwardtMichael Borgwardt
294k63426663
294k63426663
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
add a comment |
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
I wish I could give two correct answers. As a question, how can i possibly add this to the JFrame itself, because adding it to getContentPane() doesn't work -- Note it does work on a JTextField though
– AlanFoster
Apr 13 '11 at 10:58
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
@user551841: KeyListeners only work on components that actually get the keyboard focus. For global listening to key events, you probably need to work with KeyEventDispatcher or if it's just specific keys you're interested in (like hotkeys), use a key binding. Read the tutorials for details: download.oracle.com/javase/tutorial/uiswing/misc/…
– Michael Borgwardt
Apr 13 '11 at 11:07
add a comment |
If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?
However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.
Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.
add a comment |
If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?
However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.
Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.
add a comment |
If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?
However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.
Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.
If you implement an interface, that means that the implemented object can be used in any scenario where a implementation is expected. From that point of view is quite obvious that you must implement all methods of an interface, otherwise you cannot really say that you have implemented it. e.g. if I have a ICalculator interface with a Add and Subtract method, and you want only to implement Add, could the resulting class really be used by another project that needs a ICalculator?
However, I quite understand your ire about implementing some of the interfaces in the frameworks today, as they clearly break the Interface Segragation Principle. What that means is that some interfaces are clumps of several functionalities that can be used together to do something useful. E.g. if the ICalculator inteface, instead of just basic Add, Divide etc.. methods, I started adding methods like StandardDeviation that are not essential to the nature of the Calculator object.
Bottom line, yes, you have to implement each and every one of the interface's members, however, If you are sure and know that some of them will never be used, feel free to leave them empty, or even better, make them throw exceptions, so you'll be sure that never really means never.
answered Apr 13 '11 at 10:50
SWekoSWeko
25.5k65592
25.5k65592
add a comment |
add a comment |
You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.
add a comment |
You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.
add a comment |
You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.
You really should grab a good book about object orientated programming and revisit the first chapters. An interface is a completely abstract definition of methods, so you cannot implement an interface partially.
answered Apr 13 '11 at 10:40
perdianperdian
1,77641937
1,77641937
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5647800%2fimplement-an-interface-and-override-methods-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown