-1

I have been searching for an answer for a while so I am sorry if this has already been answered.

I am confused as to why inheriting empty method stubs from an interface is considered so useful in java? Is this something that is mainly used in bigger projects that i need to try to understand? Because although I know all the syntax and characteristics of interfaces, I cannot find uses for them when I am programming. I have tried to read as much as I can but this is a concept that I cannot get to 'click' in my head and I feel it is holding me back.

Or perhaps it is just case of good software design and I need to use them in certain scenarios whether I feel I need to or not?

If someone could point me in the direction of some code or preferably a project for me to do where interfaces are imperative that would be much appreciated.

mxrty
  • 21
  • 2

3 Answers3

2

Necessary? They never are. Tons of languages don't have this notion and yet they work. Interfaces are only helpful when rightly used, not necessary.

I am confused as to why inheriting empty method stubs from an interface is considered so useful in java?

It isn't. But you're only describing one effect of interfaces that you've identified.

It isn't useful either to write words like "try" and "catch" with blocks of code. What's useful is to have the concept of exception throwing and recovering from them.

Is this something that is mainly used in bigger projects

Yes, though once it's understood, it's better for almost any more-than-five-files project.

that i need to try to understand?

No need to rush, it will come with experience in its own time.

Or perhaps it is just case of good software design and I need to use them in certain scenarios whether I feel I need to or not?

It's a matter of good software design, yes. I wouldn't call it a need though, though I guess there comes a threshold where they're so much the better solution that any professional would call it a need.

kumesana
  • 2,495
  • 1
  • 9
  • 10
2

"Necessary" is a subjective term.

It is more that there are cases where concrete classes are not inherently useful.

Let's say that you write a method takes an ArrayList parameter: if you want to pass a LinkedList to it, you need to duplicate that method, changing the parameter type.

But unless there is something about the implementation of that method which really requires it to be an ArrayList specifically (*), you could just change that parameter to List, an interface, and have it accept any implementation.


(*) Things like sorting algorithms actually do care about ArrayList vs LinkedList, because of the difference in time complexity of the get and set methods (constant and linear, respectively). But even here, an ArrayList is not required: you merely want any List that has constant time complexity implementations. This is typically indicated by the concrete class implementing the RandomAccess marker interface.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Which should you use, abstract classes or interfaces? You can find the answer in the Java doc here :

https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Neerav Vadodaria
  • 317
  • 1
  • 11