• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand interfaces  RSS feed

 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
so i know interface is a cnotract, and i know that every class implementing an interface is bound to implement its method/variables.
here's what i don't understand:
i've created an interface called ID, and a class called Person implementing it. Then i've created another class called Staff,
and declaired a new okject of type Person.
It seems to me that java treats an interface as if it was an object. the boolean (p instance of ID) is true (p is Person) and i can also
creatd a method that returns an ID.
so i'm a little confused here. i guess my question is, can i look at an interface as a special kind of object? tahnks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Yotam --

An special kind of object, no. A special kind of class, yes. An object is like a physical chunk of dough. A class is like a cookie cutter with which you can cut object out in a certain shape. In that sense, an interface is pretty much the same thing: a shape that objects can have.

The differences are in the details, of course.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kind of think of an interface as a job title. if i have a KnowsCarpentry interface, I don't care if the actual person is a carpenter, a blacksmith, a programmer, or an elf. as long as they KnowsCarpentry, it does matter what the underlying class REALLY is - i can ask anyone who KnowsCarpentry to build be a bookshelf.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!