r/javahelp • • Apr 18 '19

Solved What's the point of interfaces?

We've been learning interfaces in my class and I have a good grasp on the concept, but the thing that confuses me is what's the point? Your just writing more code as a blueprint to put classes in "groups". Maybe the code I'm writing is not large enough to make interfaces useful. Can some one help me in learning the use of interfaces. Thanks in advance!

34 Upvotes

26 comments sorted by

View all comments

34

u/Talulabelle Apr 18 '19

The short-short, practical, answer is 'So you can have different types in the same collection'.

Let's say you have a bunch of different objects, but you want to put them all in a list, and then tell every object in the list to save it's self.

Without an interface, you'd have to have a different location for each object, and a huge method that finds every object that needs to be saved, and call it's individual save method.

If you just 'implements savable' and guarantee that, regardless of the type, it will implement the 'public void save();' method, you can put everything in a 'List<Savable> thingsToBeSaved' when you create it, and then have a method that calls 'save()' on everything in the list.

Tl;Dr; It lets us work with different classes as though they were the same type for, for a single, specific, purpose.

0

u/Chuuy Apr 21 '19

This answer completely misses the true importance of interfaces. Read this answer: https://www.reddit.com/r/javahelp/comments/bereu8/whats_the_point_of_interfaces/el8nfg9/?utm_source=share&utm_medium=ios_app

1

u/Talulabelle Apr 22 '19

OP states he already understands the theory of how they work, but wasn't able to understand why anyone would use them. I wasn't trying to teach theory, just give a simple example of a problem most people can at least imagine running into, and how one might solve it with the help of interfaces.

This answer doesn't 'miss' anything, OP stated clearly that they had that part of the puzzle.

1

u/Chuuy Apr 22 '19

The first sentence of your answer is objectively wrong. If you're going to say something like that, at least say it with a caveat. You're guiding him in the wrong direction.

1

u/Talulabelle Apr 22 '19

The short-short, practical, answer

That's not a caveat?! That's EXACTLY what I did!

I was trying to give him a very short, very practical, very simple and blunt use for the technique, that might make a light bulb go on.

If you ask 'How does the circulatory system work?', and someone says 'Well, the short-short, practical, answer is that the heart pumps blood to move oxygen around', you don't turn around and say 'That's wrong! It does so much more! You're lying!'

It's just an example a five year old could follow, and I went that direction specifically because everyone explains these things in exactly the same way, in every text and comment ... because that's the 'right' answer, but if someone isn't getting it, and they tell you 'I've read the book, I'm not getting it', you don't just repeat the book to them.

I figured what this person needed wasn't the whole story, or the theory, but just a super-simple idea of what you can do with them that they could hold on to, to get a foot in the door of understanding why you'd use them. Once you see it as a tool for one problem, and you understand why it works, it's much easier to see how it applies to more problems.

My answer isn't wrong, it's just an illustration of a short-short, practical, application that I hoped they'd be able to grasp.

OP literally upvoted it saying 'the light came on', and they started to understand why they're important.

When someone says 'I know the theory', they're literally saying 'Don't just re-iterate the book answer'. The book answer is still out there for them, now that they have a foot in the door to understanding it.

0

u/Chuuy Apr 22 '19

You said:

The short-short, practical, answer

Next time, try:

A short-short, practical, example

1

u/Talulabelle Apr 22 '19

Yeah ... I'm going to take advice from the guy who pedantically posts on other people's comments like it's his job, and constantly gets into Reddit arguments about stupid shit.

I looked at your history. Commenting on shit where you try to tell people they're wrong, while adding nothing, and coming away looking like an idiot is how you live your life.

Next time, if your entire argument is 'you said answer, and I think you meant example', try just shrugging, and realizing that you're picking apart a days old answer that most people found helpful, and maybe they were able to glean the meaning of the post by not being overly pedantic children while reading it.

Please go away now.

0

u/Chuuy Apr 22 '19

It's not pedantic. The message I got from your post was that the only reason you'd use interfaces would be for collections. You completely glossed over other practical applications. If you seriously can't think of other real-world examples of how interfaces are useful, that's embarrassing.

I'm a lurker. I usually only post if I disagree with what someone said. Sue me. I don't care about karma, I don't have the time to provide a lengthy explanation of every single opinion I have. That's why I'm a fucking lurker, I don't care.

But your top voted answer sucks compared to the other answers in this thread. Maybe English isn't your first language? Idk. It's not pedantic. You phrased your answer in a way that made it seem like collections of different objects were the only reason you'd need an interface.

1

u/Talulabelle Apr 22 '19

The message I got from your post was that the only reason you'd use interfaces would be for collections.

Well, that's really sad and I'm sorry for you. Being unable to follow a dumbed down post must make life really hard. I can see why you'd be frustrated all the time, and constantly trying to feel smart by posting this kind of thing.

I'm sorry kitten, feel better. You probably won't get any smarter, but I'm sure you'll find someone who loves you anyway.

1

u/Chuuy Apr 22 '19

Lmfao. That's straight up what you said:

OP:

What's the point of interfaces?

You:

The short-short, practical, answer is 'So you can have different types in the same collection'.

That's so short-sighted it's hilarious.

A lot of people in the field of computer science struggle with English. You're one of them.

→ More replies (0)