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.
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.
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.
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();
}
}
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.
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)
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.
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
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 :