What are the possible ways to add methods in interface which is already's been used by 100's of classes?
Suppose I have an interface
public interface Animal{
public void operationWalk();
}
This animal interface is implemented by many classes, now how do I add method in this interface so it should not break the functionality of classes which are implementing it.
java
add a comment |
Suppose I have an interface
public interface Animal{
public void operationWalk();
}
This animal interface is implemented by many classes, now how do I add method in this interface so it should not break the functionality of classes which are implementing it.
java
Depends what you need it to do. You could add adefault
method.
– khelwood
Nov 19 '18 at 16:08
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
1
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhandefault
andstatic
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.
– Kevin Cruijssen
Nov 19 '18 at 16:17
add a comment |
Suppose I have an interface
public interface Animal{
public void operationWalk();
}
This animal interface is implemented by many classes, now how do I add method in this interface so it should not break the functionality of classes which are implementing it.
java
Suppose I have an interface
public interface Animal{
public void operationWalk();
}
This animal interface is implemented by many classes, now how do I add method in this interface so it should not break the functionality of classes which are implementing it.
java
java
edited Nov 19 '18 at 16:19


nullpointer
43.3k1093178
43.3k1093178
asked Nov 19 '18 at 16:06
Ajay Chauhan
202215
202215
Depends what you need it to do. You could add adefault
method.
– khelwood
Nov 19 '18 at 16:08
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
1
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhandefault
andstatic
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.
– Kevin Cruijssen
Nov 19 '18 at 16:17
add a comment |
Depends what you need it to do. You could add adefault
method.
– khelwood
Nov 19 '18 at 16:08
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
1
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhandefault
andstatic
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.
– Kevin Cruijssen
Nov 19 '18 at 16:17
Depends what you need it to do. You could add a
default
method.– khelwood
Nov 19 '18 at 16:08
Depends what you need it to do. You could add a
default
method.– khelwood
Nov 19 '18 at 16:08
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
1
1
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhan
default
and static
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.– Kevin Cruijssen
Nov 19 '18 at 16:17
@AjayChauhan
default
and static
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.– Kevin Cruijssen
Nov 19 '18 at 16:17
add a comment |
3 Answers
3
active
oldest
votes
You can use default method in Java 8
Read about it here
add a comment |
Since you have had a java-8-tag, you have access to the default
keyword for an interface
method, which is new since Java 8.
So you could modify it to something along the lines of:
public interface Animal{
public void operationWalk();
default void doSomething(){
// Your code here
}
}
This default
method can be added to the interface, without having to add the implementations to your child classes that use this interface.
Note that default
methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static
method in the interface instead. Here a bit more info about default
vs static
methods in interfaces.
If you are using a version prior to Java 8, I'm afraid the only options you have are:
1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class
implementing the Animal-interface
with the implementation of the new method, and then change each of the 100+ classes to extend this abstract
class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract
class, so if any of your classes implementing the Animal
-interface are already extensions of other classes, this option isn't possible.
Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default
keyword for interfaces
that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences betweendefault
andstatic
methods in interfaces.
– Kevin Cruijssen
Nov 19 '18 at 16:15
add a comment |
Just to add a few options to what other people have already mentioned:
If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.
If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.
A third possibility is to have some classes have "empty" implementations:
public void operationWalk() {
}
On the plus side, at least this is just copy-paste, so it won't take all that long.
A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.
public void operationWalk() {
// Throw some exception indicating that this operation doesn't apply here
}
This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.
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%2f53378548%2fwhat-are-the-possible-ways-to-add-methods-in-interface-which-is-alreadys-been-u%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use default method in Java 8
Read about it here
add a comment |
You can use default method in Java 8
Read about it here
add a comment |
You can use default method in Java 8
Read about it here
You can use default method in Java 8
Read about it here
edited Nov 19 '18 at 16:14
answered Nov 19 '18 at 16:08
Tengo Chilingarashvili
513
513
add a comment |
add a comment |
Since you have had a java-8-tag, you have access to the default
keyword for an interface
method, which is new since Java 8.
So you could modify it to something along the lines of:
public interface Animal{
public void operationWalk();
default void doSomething(){
// Your code here
}
}
This default
method can be added to the interface, without having to add the implementations to your child classes that use this interface.
Note that default
methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static
method in the interface instead. Here a bit more info about default
vs static
methods in interfaces.
If you are using a version prior to Java 8, I'm afraid the only options you have are:
1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class
implementing the Animal-interface
with the implementation of the new method, and then change each of the 100+ classes to extend this abstract
class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract
class, so if any of your classes implementing the Animal
-interface are already extensions of other classes, this option isn't possible.
Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default
keyword for interfaces
that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences betweendefault
andstatic
methods in interfaces.
– Kevin Cruijssen
Nov 19 '18 at 16:15
add a comment |
Since you have had a java-8-tag, you have access to the default
keyword for an interface
method, which is new since Java 8.
So you could modify it to something along the lines of:
public interface Animal{
public void operationWalk();
default void doSomething(){
// Your code here
}
}
This default
method can be added to the interface, without having to add the implementations to your child classes that use this interface.
Note that default
methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static
method in the interface instead. Here a bit more info about default
vs static
methods in interfaces.
If you are using a version prior to Java 8, I'm afraid the only options you have are:
1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class
implementing the Animal-interface
with the implementation of the new method, and then change each of the 100+ classes to extend this abstract
class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract
class, so if any of your classes implementing the Animal
-interface are already extensions of other classes, this option isn't possible.
Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default
keyword for interfaces
that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences betweendefault
andstatic
methods in interfaces.
– Kevin Cruijssen
Nov 19 '18 at 16:15
add a comment |
Since you have had a java-8-tag, you have access to the default
keyword for an interface
method, which is new since Java 8.
So you could modify it to something along the lines of:
public interface Animal{
public void operationWalk();
default void doSomething(){
// Your code here
}
}
This default
method can be added to the interface, without having to add the implementations to your child classes that use this interface.
Note that default
methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static
method in the interface instead. Here a bit more info about default
vs static
methods in interfaces.
If you are using a version prior to Java 8, I'm afraid the only options you have are:
1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class
implementing the Animal-interface
with the implementation of the new method, and then change each of the 100+ classes to extend this abstract
class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract
class, so if any of your classes implementing the Animal
-interface are already extensions of other classes, this option isn't possible.
Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default
keyword for interfaces
that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.
Since you have had a java-8-tag, you have access to the default
keyword for an interface
method, which is new since Java 8.
So you could modify it to something along the lines of:
public interface Animal{
public void operationWalk();
default void doSomething(){
// Your code here
}
}
This default
method can be added to the interface, without having to add the implementations to your child classes that use this interface.
Note that default
methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static
method in the interface instead. Here a bit more info about default
vs static
methods in interfaces.
If you are using a version prior to Java 8, I'm afraid the only options you have are:
1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class
implementing the Animal-interface
with the implementation of the new method, and then change each of the 100+ classes to extend this abstract
class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract
class, so if any of your classes implementing the Animal
-interface are already extensions of other classes, this option isn't possible.
Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default
keyword for interfaces
that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.
edited Nov 19 '18 at 16:56
answered Nov 19 '18 at 16:10


Kevin Cruijssen
4,71573783
4,71573783
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences betweendefault
andstatic
methods in interfaces.
– Kevin Cruijssen
Nov 19 '18 at 16:15
add a comment |
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences betweendefault
andstatic
methods in interfaces.
– Kevin Cruijssen
Nov 19 '18 at 16:15
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
There is also the option to use static methods if we are already using Java 8.
– Murat Karagöz
Nov 19 '18 at 16:12
@MuratKaragöz Edited it to the answer, including a link of the differences between
default
and static
methods in interfaces.– Kevin Cruijssen
Nov 19 '18 at 16:15
@MuratKaragöz Edited it to the answer, including a link of the differences between
default
and static
methods in interfaces.– Kevin Cruijssen
Nov 19 '18 at 16:15
add a comment |
Just to add a few options to what other people have already mentioned:
If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.
If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.
A third possibility is to have some classes have "empty" implementations:
public void operationWalk() {
}
On the plus side, at least this is just copy-paste, so it won't take all that long.
A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.
public void operationWalk() {
// Throw some exception indicating that this operation doesn't apply here
}
This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.
add a comment |
Just to add a few options to what other people have already mentioned:
If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.
If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.
A third possibility is to have some classes have "empty" implementations:
public void operationWalk() {
}
On the plus side, at least this is just copy-paste, so it won't take all that long.
A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.
public void operationWalk() {
// Throw some exception indicating that this operation doesn't apply here
}
This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.
add a comment |
Just to add a few options to what other people have already mentioned:
If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.
If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.
A third possibility is to have some classes have "empty" implementations:
public void operationWalk() {
}
On the plus side, at least this is just copy-paste, so it won't take all that long.
A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.
public void operationWalk() {
// Throw some exception indicating that this operation doesn't apply here
}
This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.
Just to add a few options to what other people have already mentioned:
If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.
If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.
A third possibility is to have some classes have "empty" implementations:
public void operationWalk() {
}
On the plus side, at least this is just copy-paste, so it won't take all that long.
A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.
public void operationWalk() {
// Throw some exception indicating that this operation doesn't apply here
}
This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.
answered Nov 19 '18 at 18:03


EJoshuaS
7,103102748
7,103102748
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53378548%2fwhat-are-the-possible-ways-to-add-methods-in-interface-which-is-alreadys-been-u%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
Depends what you need it to do. You could add a
default
method.– khelwood
Nov 19 '18 at 16:08
How to do if my program is running on lower than 8?
– Ajay Chauhan
Nov 19 '18 at 16:14
1
@AjayChauhan You can not do that below Java 8. You would need to create a new interface.
– Murat Karagöz
Nov 19 '18 at 16:16
@AjayChauhan
default
andstatic
methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.– Kevin Cruijssen
Nov 19 '18 at 16:17