Web Hosting Talk







View Full Version : what the heck is interface used for?


grabmail
03-10-2006, 08:18 AM
I don't get it. Hope someone can enlighten me.

interface Shape {
function getCircum() {}
function getArea() {}
}


class Square implements Shape {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}

class Circle implements Shape {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}





OKKKK.... i don't get it. What's the point? Why can't you just NOT IMPLEMENT Shape?

Isn't the above codes the same as the one below?

class Square {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}

class Circle {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}


IF yes, WHAT's the point of INTERFACE?

ProperHost
03-10-2006, 08:46 AM
I don't get it. Hope someone can enlighten me.

interface Shape {
function getCircum() {}
function getArea() {}
}


class Square implements Shape {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}

class Circle implements Shape {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}





OKKKK.... i don't get it. What's the point? Why can't you just NOT IMPLEMENT Shape?

Isn't the above codes the same as the one below?

class Square {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}

class Circle {

function getCircum() {return $this->circm}
function getArea() { return $this->area}
}


IF yes, WHAT's the point of INTERFACE?

In short, an interface is used to establish a kind of "communication protocol" between classes, in order for different classes to interact by accessing mutual behaviors and methods without the need to enforce a class relationship.

Others; please correct me if I'm wrong..

grabmail
03-10-2006, 10:19 AM
Ok. How do they actually INTERACT?

I don't get it. Interface don't pass any values or methods.

The only use i can think of is that Interface force you to implement the methods in the interface. Hence, this prevents accidental deletion of methods in implemented classes.

Correct me if i'm wrong.

grabmail
03-10-2006, 10:56 AM
OK. I GET it now. You use Interface to for your child classes to implement the methods in the interface, which prevents accidental deletions of these methods.

Burhan
03-10-2006, 10:59 AM
You won't see the benefit of interfaces, unless you are using two different components/classes that are written by two different people.

Interfaces are a way of guranteeing that a method will be available in a class, and that it will do what it advertised. If you are a class based on a framework, then anyone else knows that you must provide methods that are listed in the class; this helps in utilizing your objects.

They are created for interoperability; so unless you are doing something that is not academic, you will not realize the benefits of interfaces.

deuce868
03-10-2006, 01:56 PM
Pick up a good book on patterns and look for where they talk about how extending a class is never the first choice.