Interface in java

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
Ummm...that's a very general question. An interface is like a class, but with no variables, and without code blocks for the methods.

Need more? Let me Wikipedia that for you.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Java 8 now has Default Methods, so I suppose an interface *could* have code. Vague question is vague.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Java 8 now has Default Methods, so I suppose an interface *could* have code. Vague question is vague.

The line between abstract class and interface is getting kinda blurry with Java 8, but I like it!
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
If Wikipedia didn't help, please explain your situation in more detail.

And is this homework?
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
Class interface?
user interface?
graphical user interface?

You need to provide some hints as to your "situation". Most of us are not very good at 20 questions
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
purbeast , HA

Interface is what you want to do. Implementation (class) is how you do it.

Unless you're in Java 8, where an interface may also tell you how to do it. [Worst] design decision in all of Java, yet. Well, that and Functional Interfaces, which make for some REALLY ugly lambdas.

No swearing in the technical forms, please -- Programming moderator Ken g6
 
Last edited by a moderator:

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
I refuse to use java 8 then.

Wait, multiple parents are now possible?

Yes and no. An interface can now supply a default implementation of a method, but it cannot maintain state in any way. So interfaces still can't contain instance variables. What these new Default Method interfaces can do, though, is also include some abstract methods that the concrete class must implement. A Default Method can use the abstract methods, so that lets you blur the lines of multiple inheritance a bit.

Personally, I think it's just fancy spaghetti code and/or a failed abstraction. It's a very poor composition/mix-in substitute that doesn't add much to the language other than cause confusion. These new interfaces were introduced to the language basically just to support the new Streams API.

Java feels dated. I don't understand the "write once, run anywhere" approach. Because of this, we're stuck with clunky compromises to maintain compatibility with ancient versions of Java that nobody uses. If composition and functional programming were actually important, they should be built into the language itself, rather than making them just some syntax sugar. Anders Helsjberg got it right, methinks.

So yea, a year ago, we could have ended the thread with reallyscrued's answer. Now, with Java 8, interfaces are needlessly "flexible."
 
Sep 29, 2004
18,656
68
91
I read an article and the reason that this new interface construct was added was because of Lambdas. I forget the full rational but without this construct, Lambdas would not be possible.
 
Sep 29, 2004
18,656
68
91
This, for the love of God.

Leave the kid alone. He doesn't yet realize that Google can do 99% of his homework. Actually, it's how I do a lot of things these days.

I can come up wit ha method to take remaining milliseconds and convert it to remaining years, months, days and hours .... or I can Google the solution and copy in paste it all in about 1 minute.

Anyways ... want ot know how to use interfaces in Java? Google it? Wikipeida probably even has examples.

Best common use for interfaces that we use at work frequently? Write an interface that interacts with an Oracle database. And for unit testing implement the same interface but use HSQLDB behind the scenes. Design the software so that it is easy to swap the implementations.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Leave the kid alone. He doesn't yet realize that Google can do 99% of his homework. Actually, it's how I do a lot of things these days.

Yes, I Google too. Sometimes I even post messages on forums. And then when people respond I engage with them in a hopefully intelligent discussion about my problem.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
I read an article and the reason that this new interface construct was added was because of Lambdas. I forget the full rational but without this construct, Lambdas would not be possible.

So that's true, but it's not the complete truth. Functional Interfaces were the least intrusive way of introducing lambas to Java while maintaining backward compatibility all the way to Java of old. So basically, they ended up with a crappy, clunky implementation of lambda expression support. C# has it made! :)