0

Seems like I am missing something here. I don't understand what are they good for?
Meaning: simple inheritance between classes, can give the same results, or not?!

And, to be more precise:
Interface is - as I understand it - a .. list of functions names. and nothing more.
So, what is the added value?

As to Abstract, Seems that it is a simple class that can't be instantiated at all, and all its use is to force the child-classes, to inherit some methods.
Again - what is the added value?

Both behaviors can be achieved using regular classes. or just avoiding it (when it comes to the Interface object)

also, please don't point me to duplicates, i've read them all. they explain what are abstract and interface, but not answering my question.

thanks.

yossi
  • 3,090
  • 7
  • 45
  • 65
  • please, clarify the question, because, I see 2 questions – sergio Nov 13 '14 at 11:26
  • And I don't see a question fitted for SO at all.. If the other resources explain, what these constructs are, they should either explain, what they are good for or it is trivial. If both is not the case, you need to look for other resources – kero Nov 13 '14 at 11:28
  • possible duplicate of [What is the difference between an interface and abstract class?](http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class) – Jimbo Nov 13 '14 at 11:55
  • 1
    You're building a system and you want to **maintain control** of what you build, and the object API's (public methods) you write. If you want to force someone to implement specific methods to allow polymorphism (& switching out classes in the future easily) then tell them to implement your interface and it'll all continue working when you switch out. If you want to force an interface, *as well as some functionality*, use an abstract class with abstract methods: you can define some *base* functionality and any abstract methods have to be overridden, just like an interface. – Jimbo Nov 13 '14 at 11:56
  • 1
    An `interface` provides a list of functions which **need** to be implemented. You cannot do this with a regular class. The advantage of an `abstract` class is, you can't spawn something abstract. Woldn't be working with a regular class. You should think big, big projects need those mechanism to clearify relations and get everything to work properly. – Daniel W. Nov 13 '14 at 12:04
  • You need to reserch, there a lot of info about this. Also you need to look about Design Pattern, you will clarify also some dark points – sergio Nov 13 '14 at 12:08
  • Yes @sergio, i can and i did. but some of the research is asking in Stack' and get answers prom professionals. i think... – yossi Nov 13 '14 at 12:32
  • @Jimbo & DanFromGermany - thanks. – yossi Nov 13 '14 at 12:33

1 Answers1

1

Abstract is inheritance

A Duck is an Animal

Interface is a contract

A Duck Implements Swim, A Duck Implements Fly

The Duck must have any methods which are abstract in Animal, and All methods listed in Swim and Fly

  • Update

The way I understand it is that it's an easy way to abstract further than the inheritance model. Machine and Animal Abstracts can both have their own abstract methods that detail some element of motion.

Or specific child classes that know that they can Fly, or Swim, can implement an Interface themselves.

Planes, and Ducks share a lot of their requirements for flight .. a suitable take/off landing area, a means to avert a collision, how to ascertain height, how to ascertain proximity to other objects of the same type (ducks like proximity, planes don't).

In this case, machines and animals are both things, so perhaps we just make thing inherit from an abstract that has flight and swim methods - but then you could ultimately end up supporting slightly odd stuff like $theFish->walk()

Instead though we could keep the inheritance model clean, and just implement an interface as required that knows nothing about the object, but can direct the object how to define itself good for purpose (in this case flight/swimming).

Cliffordlife
  • 488
  • 5
  • 13
  • 1
    He said: " ... i've read them all. they explain what are abstract and interface, " – sergio Nov 13 '14 at 11:29
  • @Cliffordlife Thanks. I can see the logic, but i am not sure that "keep it clean" is good enough reason to create a language object. i am sure that it had more reasons – yossi Nov 14 '14 at 09:47