Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused about JAVA interface [duplicate]

Tags:

java

interface

Possible Duplicate:
What does it mean to “program to an interface”?
Interface vs Abstract Class (general OO)

I'm new to learn JAVA and now I'm confused about interface. I have searched and read many materials but still not clear.

When I try to find some information about interface, I see many people talked about the relationship between interface and abstract class. But I even don't know why they contrast these two. Because I think abstract class is used to tell other people you can not create an object of this abstract class and if you want, you must modify the abstract class. This is something about inheritance, right?

But I don't know the meaning of interface. There is a interface a, and if a class B is going to implement the interface a, it must use the reserved word class B implements a, and then complete all the methods that the interface requires. But my question is, if class B have to complete all the methods by itself, what's the meaning of interface? I think we don't need it. I don't understand it very much. I read many sentences like: "interface can reflect the core thought of object-oriented language", "interface can help make the program easier" and so on. But I can not really understand the meaning.

So, does anyone can show me some examples to let understand interface? Or you can tell me some useful links or the books that describe the interface clearly. I really hope to figure it out. THANK YOU!

like image 540
lkkeepmoving Avatar asked Oct 20 '12 22:10

lkkeepmoving


1 Answers

Suppose you have a Car class and Vegetable class which is unrelated in real life and there is a common behaviour called wash(). Because we can wash a car and wash a vegetable too. But washing a car and washing a vegetable is totally different process/behaviour.

For ex: Car should be washed with a power pump, Vegetables under your kitchen sink. So the way of washing is different. So you make the washing process as a method wash() in the interface say Washable and you implement them in both Car and Vegetable class.

interface Washable {

 public void wash();

} 

public class Car implements Washable {

 public void wash() {

   // wash the car with a power pump

 }


}

public class Vegetable implements Washable {

public void wash() {

   // wash the vegetable under a kitchen sink

 }


}

As a person, you would want to wash a car as well as vegetable.

public class Person  {


Washable washableObject = new Car();

washableObject.wash();

washableObject = new Vegetable();

washableObject.wash();




}
  1. So interface is a way to connect unrelated classes which has a common behavior.But the behavior will be differently implemented or can be changed in future.

  2. One day you decide to change the way you wash a Car.Suppose you have purchased a "car washing machine". So the implementation changes inside the method wash() in the Car class.

    public class Car implements Washable {

      public void wash() {
    
       // wash the car with my new car washing machine !!
    
     }
    

    }

But as a Person , you still call the wash() method. The way the wash() method is being implemented changed ( washing the car with your new car washing machine ), this implementation change did not affect your Person class.

Hope you are clear why we use interfaces now.

like image 115
Tito Avatar answered Oct 13 '22 18:10

Tito