"Hello, Amigo! Soon you'll be dreaming of interfaces. So I wouldn't sleep if I were you. No way! Ha, ha! Here are a couple of little exercises for you. May interfaces ever remain in your brain."
I have a general question about this topic. If you have a class Human (for example in the last exercise) implementing the interfaces, must such class always be abstract and public? Or could it be something else? Static if the interface is static? Thanks in advance!
I have two questions:
1. How come interface methods can't have a body(the curly braces)? How do you execute things in a method then?
2. Why do methods of a class (the inherited abstract ones) need to be public? For example:
public interface CanFly {
void fly();
}
public class Airplane implements CanFly {
public void fly() { /* needs to be public otherwise it says "attempting to assign weaker access privileges; was public" */
}
}
Answering 1: Interface is just saying, "Look who ever's gonna implement me, You need have these methods and you gotta write it yourself!". This is the point of interface! We'll see its uses later.
Answering 2:
public interface CanFly {
void fly();
}
You cant make the void fly() a private because it itself is a public. EVERY method in interface has a hidden "public abstract" so void fly() is actually "public abstract void fly();". When u make it private, u are literally attempting to assign a weaker access to it but you're the one implementing it.
<s**t you're higher level than me but i already wrote this down so i won't be deleting >
Lol thanks for clarifying though! Yeah if all the child classes all have the same method and you want to implement it in the parent class, using abstract classes work. Thanks!
Still trying to wrap my head around how implementing the methods from an interface is going to be useful in the future since we can't write any actual code in the method.
It helps with flexibility in larger applications to create loosely coupled code. You can on the fly create a new class and implement the interface methods and deploy the software almost immediately with minimal changes to the application as a whole.
So, because of the interfaces I have to write at least twice some method names (for example I write at the CanFly() interface the fly() method and again write it at the specific class, for example at the Airplane). Why on the Earth is it so useful?? Because for me it seems like a pre-note for myself to what should I write at some classes later. It seems for me redundant... :/
I was wondering about this too. But as I understand it (if I'm wrong please someone corrects me) it extends the power of polymorphism and gives you additonal "type" (role) of each object.
For example if you have Animal [] array you can put in there Animal objects and every other object that extends from Animal Class (for example a Dog). But you cannot put something outside inheritance tree (for example a Chair - object which extends from different tree - let's say Furniture Class).
However if the Dog and the Chair Classes implements fourLeg interface, even though they are different classes from different trees, Dog and Chair object can be put together in fourLeg [] array. This is because they have implemented common interface. Therefore Dog object is instance of Animal Class, Dog Class and fourLeg (interface), while Chair object is instance of Furniture Class, Chair class and fourLeg (interface).
The same goes for methods (if you have method with fourLeg parameter (void doSomething(fourLeg a) {...}) you can pass there Dog object or Chair object).
So probably with one inheritance tree interface is not so usefull but with many it is helpful.
Yeah, now I get it, that it's a kind of solution for the "there is no multiple-inheritance in java" problem, because with interfaces you don't have to face with diamond-problem. but idk... it still feels like to me redundant. it's good, that there is a grouping solution with interfaces (like classes), therefore solves that kind of problem that is mentioned by yourself, but... I have to write the method names over and over again :D I guess something for something.
Well, as it is stated in article on the next page (which, when I replied, I did not know of), you can always put default method in interface which will save you some time. For me it was (and still is) also a little bit unintuitive. Let's hope that some day, it will be perfectly natural for us :)
GO TO FULL VERSION