tags:

views:

143

answers:

7

Please explain using plain english why we need and why we use Interface in Object Oriented development.

I am in serious confusion.

[+] I'm working on Java. Please provide sample code in Java if any.

Thanks all.

+2  A: 

you are providing just the things to do by interface ,how it will get done that will be covered in implementation.

Suppose there is an interface Driver you will declare a method drive();

now suppose a car driver implements this method then he can write his code there.
If a pilot implements drive method then his code..

I hope its clear.

org.life.java
The interface name should be `Driver`, not `Drive`.
Willi
@Willi yeah, updated, drive is method that driver knows.
org.life.java
+3  A: 

Did you check, the part with Java Interface you have an explanation and some Java example

http://www.javabeginner.com/learn-java/java-abstract-class-and-interface

Example 1: Below is an example of a Shape interface

interface Shape {

    public double area();
    public double volume();
}

Below is a Point class that implements the Shape interface.

public class Point implements Shape {

    static int x, y;
    public Point() {
        x = 0;
        y = 0;
    }
    public double area() {
        return 0;
    }
    public double volume() {
        return 0;
    }
    public static void print() {
        System.out.println("point: " + x + "," + y);
    }
    public static void main(String args[]) {
        Point p = new Point();
        p.print();
    }
}
Adnan
Agreed. Interfaces are useful because they make *polymorphism* possible. I.e. calling a function on some object of which you do not know the precise type, but you know that it implements some interface.
Sjoerd
+1  A: 

Here is the entry from JavaRanch's Java Beginner FAQ section. It might be helpful. Moreover, go to JavaRanch. For Java beginners, particularly, there is no other better substitute.

Adeel Ansari
A: 

Hi,

A interface is used to describe what 2 objects have in common but implement differntly. The example given by mybook was with ducks.

Suppose u have 2 ducks

  • A normal alive one

  • and a wooden one

And an interface Ifly with the method fly();

Both are able to fly. So the Fly method goes in the interface. Because when u implement an interface, U're bound to implement the functions in that interface. (it's like a contract!)

so Duck implements IFly and in its fly method all it does write to the console saying: I Can fly.

While the wooden duck one says "I Can't fly".

then when u do this:

(C# sorry don't know java)

IFLy duck = new Duck() duck.fly(); -- This will write I Can fly. Ifly woodenduck = new WoodenDuck() woodenduck.fly()

that says I Can't fly.

In short, A interface describes what an object HAS to implement so u could see it as an contract.

Hope this makes it a bit clearer, had a lot of trouble with this in the beginning too.

(srry can't explain really good)

Emerion
Sorry to ask can you explain to me why we need a contract coupled in the first place while we can just implement individual methods within its own class. I've read lots of article and good java books. Still, I was not satisfied. I hope someone could explain in a different story why interface should exist. You answer might enlightened others too.
Mohd Alif
Ok let me try it this way then, Interfaces are really important in OOP (object oriented programming) because, the allow good encapsulation! (this also answers your question) A object only sees the interface and doesn't have to care how that object implements or processes the function. Another great example for Interfaces is that they can be used in teams to restrict other programmers to implement certain funtions. (a contract) Because if that class of him or her doesn't implement the interface the compiler will complain. and the project will not compile. Hope this clears things a bit.
Emerion
+1  A: 

An interface (in java) is an uninstatiable abstract class (it has no constructors) that provides public methods that all subclasses that inherits them must implement.

Also, one advantage of having inheritance is to facilitate with multiple inheritance.

The Elite Gentleman
A: 

Do you mean why do we need interfaces or why do we not using only abstract classes? Interfaces provide:

1)Abstraction and dependency inversion. High-level classes don't care about concrete low-level class they work with. More abstration you have, more flexibility you get. For example, you can completely rewrite level that works with DB and you keep untouched other levels of your app because they work with interfaces but not with concrete implementation.

2)In Java interfaces provide multiple inheritance

Donz
A: 

Honestly , you should invest time in reading good book on Java. I recommend The Java Complete Reference by Herbert Schildt.

Start by understanding the following terms and their significance in OO context :

  1. Type
  2. Contract published by a type
  3. Implementation
  4. Polymorphism
Bhaskar