IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBRole-Based T...Coding & OOObject Orien...   Print This     
  From the March 2016 Issue of Prestwood eMag
 
Coder Object Orientation (OO):
Interface
 
Posted 10 years ago on 10/19/2008 and updated 9/20/2009
Take Away:

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

Coder Definition Flashcard:
 A flashcard from our Coder Flashcards Library
 A definition from our Coder Definitions Page

KB101333



Interface -

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.


Interfaces are a type of class association. It promises to support the same behavior across multiple classes without the baggage of a particular implementation.

Essentially, you define an interface of abstract methods and properties and then add the interface to a class' definition and fulfill the interfaces' methods and properties. You can design classes in different descendant lines with common methods and properties without an interface but an interface ensures you implement the methods and properties using the same names and parameters.

Important Points

  • You do not create an object instance from an interface like you do with a class (you instantiate objects from classes that support the interface).
  • Every class that wishes to support an interface implements all methods and properties specified by the interface. 

Conceptual Example

If you are designing and programming a series of robots, you might create several interfaces so all your robot lines have similar command sets. You might design the following interfaces:

  • ITalk - basic talking commands like speak.
  • ITalkSmart - Inherits ITalk plus adds a database of responses to specific words and phrases.
  • IMove - adds basic movement such as walk, arm movement, etc.
  • ILife - adds basic life-like properties such as Birthday, DeathDay, Age, Weight, Height, etc.
  • IHuman - adds human-like properties such as Sex, HairColor, EyeColor, etc.

Once you've designed your interfaces, you might create a series of robots that implement all or some variation of the interfaces above.

Interface-Based Polymorphism (Substitutability)

Substitutability allows a descendant class to be used anywhere an associated parent class is used. In object oriented programming a variable could refer to one object at one time and then another object another time. This allows the designer of software to create both a dog.run and a cat.run methods and then decide at runtime whether the variable will be a dog or a cat. Interfaces are a common coding element used to implement this type of polymorphism.

Interfaces allow for horizontal class design.

In a class tree, inheritance is used to design classes vertically with "is-a" relationships. You add functionality from top to bottom adding methods and properties to descendant classes. Interfaces allow you to design horizontally across your class tree using a "behaves-as" or "looks-like" relationship insisting certain classes implement a common interface.

Is support of interfaces a kind of multiple inheritance?

No, not in my opinion because interfaces are about specifying common interfaces but are missing implementation details. Some argue that interfaces are a form or type of multiple inheritance, but to me it is not. That would be like saying VB 6 Classic supported inheritance because it allowed you to establish a single abstract class and then implement it in several classes. A nice feature, but it just wasn't even close to a complete implementation of inheritance.

More Info

Code:  Access VBA Interfaces
Code:  ASP Classic Interfaces (Not Supported)
Code:  C# Interfaces (interface)
Code:  C++ Interfaces (No, but mimic it.)
Code:  Delphi Interfaces (IInterface, TInterfacedObject)
Code:  Delphi Prism Interfaces
Code:  Java Interfaces (Yes)
Code:  VB Classic Interfaces
Code:  VB.Net Interfaces (Interface, Implements)

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1173A1
Enter key:
Definition Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101333 Counter
21117
Since 10/19/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2018 Prestwood IT Solutions.   [Security & Privacy]