• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classes and Interfaces  RSS feed

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These questions may be obvious, but I need to ask them anyway for my own clarification.

What is an Interface

Classes don't "have" Interfaces, right? Classes "implement" interfaces.
Interfaces aren't written inside a class definition, are they? The ones (Interfaces) I've written are always separated out of the class definition.

What is Object Oriented Programming

The OO concepts outlined by Sun are not necessarily what Microsoft calls Object Oriented Programming, is this correct?


Sorry, this post snuck in before I finished writing my question. Yes, it took me awhile to finish the question, I was trying to do some research before asking a dumb question.
[ August 25, 2006: Message edited by: leo donahue ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might think of an interface as a "contract" requiring that certain members (fields and methods) are available. Classes that implement an interface must fulfill the contract by implementing the methods.

For example, if an Animal interface declares a method makeNoise(), then any class implementing the interface must be able to makeNoise. How they do it is up to the particular Animal -- for example, a Cat would implement this method differently than a Dog.

With this contract in place, we can safely treat all Animals the same way. For example, a PetHospital class (or interface) can define a Collection of Animals, knowing that makeNoise can be called on any of these elements -- even classes of Animals that aren't yet defined.

In general, instances of classes that implement interfaces are also the interface type. For example, a Cat is an Animal.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it fairly common to write classes containing only various nested Interfaces?

For example, we wouldn't create a Dog class and within the Dog class create a Dog Interface and a Pet Interface where the properties and methods of the Pet Interface are common to all Animals? Wouldn't we would make the Pet Interface a "stand-alone" source file?
[ August 25, 2006: Message edited by: leo donahue ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...Wouldn't we would make the Pet Interface a "stand-alone" source file?
Yes this would be a much more common and useful scenario.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!