473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Basic Concept Question Why Interface?

jm
Consider:
http://msdn.microsoft.com/library/de...ycomponent.asp
// Code for the IAccount interface module.
public interface IAccount
{
void PostInterest();
void DeductFees(IFeeSchedule feeSchedule);
}
class BusinessAccount : IAccount
{
void IAccount.PostInterest()
{
// Code to post interest using the most favorable rate.
}

void IAccount.DeductFees(IFeeSchedule feeSchedule)
{
// Code to change a preferred rate for various services.
}
}
Note An interface is a contract. You must implement all of the
properties and methods in the interface.

I do not understand why Interface was necessary. Why not just have
the class BusinessAccount and two functions in it PostInterest() and
DeductFees()?

Thank you.
Nov 16 '05 #1
4 8560
There was a thread last week that dealt with this exact question. You can
google for it.
Basically, an interface tells a class that another class implements all the
methods contained in an interface, therefore, you do not have to know what
type of object it is, only that it implements the interface.

In the below example, if you did not implement the interface, if you had
another class with methods PostInterest and DeductFees, you would have to
check for type before converting them to their type and then calling their
methods.
With the Interface, you only need to cast it to the interface (if it is not
passed as the interface type) before calling the methods.

HTH
JB

"jm" <jo*************@yahoo.com> wrote in message
news:c6**************************@posting.google.c om...
Consider:
http://msdn.microsoft.com/library/de...ycomponent.asp

// Code for the IAccount interface module.
public interface IAccount
{
void PostInterest();
void DeductFees(IFeeSchedule feeSchedule);
}
class BusinessAccount : IAccount
{
void IAccount.PostInterest()
{
// Code to post interest using the most favorable rate.
}

void IAccount.DeductFees(IFeeSchedule feeSchedule)
{
// Code to change a preferred rate for various services.
}
}
Note An interface is a contract. You must implement all of the
properties and methods in the interface.

I do not understand why Interface was necessary. Why not just have
the class BusinessAccount and two functions in it PostInterest() and
DeductFees()?

Thank you.

Nov 16 '05 #2
JM... If BusinessAccount was the only class that supported the functions
PostInterest and DeductFees using an interface would be overkill. If you
have
a set of methods that will be implemented across many classes, a so
called
mixin class, then an interface is helpful. An example is loading a
plugin at
runtime. As long as the dynamically loaded class implements the
interface
you can load the class at runtime and call methods in the interface.

Regards,
Jeff
I do not understand why Interface was necessary. Why not just have

the class BusinessAccount and two functions in it PostInterest() and
DeductFees()?<
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
The answer can be found by digging deeper into Design Patterns.

Consider: http://home.earthlink.net/~huston2/dp/patterns.html
(just one of thousands of links about design patterns, although Vince Huston
is a bit more "convinced" than most folks. I like the site for its
tutorials).

If you drill into each of the design patterns under the Gang-of-Four
section, you will begin to notice two things:

1) These are EXCELLENT solutions to some really common problems, (ranging
from simple to really difficult problems). If you really take the time to
learn these patterns, your programming will improve dramatically, and

2) Nearly every one of these design patterns REQUIRES the use of interface
definitions in order to create the concept of a contract where any object
that implements the contract can be substituted for any other object that
also implements the contract. (this ability to substitute forms the
implementation foundation of the Liskov Substitution Principle).

In other words, delve deeper. Keep asking questions. Pick up a copy of
"Design Patterns Explained" by Alan Shalloway and James Trott. Then read
it. Then read it again.

And enjoy this journey... it's a fun ride.
--- Nick

"jm" <jo*************@yahoo.com> wrote in message
news:c6**************************@posting.google.c om...
Consider:
http://msdn.microsoft.com/library/de...ycomponent.asp

// Code for the IAccount interface module.
public interface IAccount
{
void PostInterest();
void DeductFees(IFeeSchedule feeSchedule);
}
class BusinessAccount : IAccount
{
void IAccount.PostInterest()
{
// Code to post interest using the most favorable rate.
}

void IAccount.DeductFees(IFeeSchedule feeSchedule)
{
// Code to change a preferred rate for various services.
}
}
Note An interface is a contract. You must implement all of the
properties and methods in the interface.

I do not understand why Interface was necessary. Why not just have
the class BusinessAccount and two functions in it PostInterest() and
DeductFees()?

Thank you.

Nov 16 '05 #4

On 15 Jun 2004 03:54, "Nick Malik" wrote:
The answer can be found by digging deeper into Design Patterns.

Consider: http://home.earthlink.net/~huston2/dp/patterns.html
(just one of thousands of links about design patterns, although Vince Huston
is a bit more "convinced" than most folks. I like the site for its
tutorials).
Very true. They are a great source.

If you drill into each of the design patterns under the Gang-of-Four
section, you will begin to notice two things:

1) These are EXCELLENT solutions to some really common problems, (ranging
from simple to really difficult problems). If you really take the time to
learn these patterns, your programming will improve dramatically, and
Very true again.
2) Nearly every one of these design patterns REQUIRES the use of interface
definitions in order to create the concept of a contract where any object
that implements the contract can be substituted for any other object that
also implements the contract. (this ability to substitute forms the
implementation foundation of the Liskov Substitution Principle).
Wrong. I can't think of one of the GoF patterns which is presented in
terms of an interface and not inheritance. And the LSP is specifically
talking about Inheritance although it applies to interfaces too. (It's
the Type/SubType phrasing I guess.)

In other words, delve deeper. Keep asking questions. Pick up a copy of
"Design Patterns Explained" by Alan Shalloway and James Trott. Then read
it. Then read it again.

Why not got for the source? Get the GoF book.


Simon Smith
simon dot s at ghytred dot com
http://www.ghytred.com/NewsLook - Usenet for Outlook
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: CHANGE username to westes | last post by:
What are the most popular, and well supported, libraries of drivers for bar code scanners that include a Visual Basic and C/C++ API? My requirements are: - Must allow an application to be...
19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
7
by: Garma | last post by:
How to implement encapsulation and scope like C++ in C? What are differences between Declaration and Definition in C? My understanding is: /*this is declaration and definition*/ int a; ...
0
by: Kristof Thys | last post by:
I'm creating a ASP.net webservice written in c++, so I'm posting my question on both the aspnet and vc newsgroups. I hope somebody can help me... What I want to do seems basic c++ to me... ...
21
by: Philipp | last post by:
Hey, did anyone have a good paper about the opject orianteted concept? wishes
21
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText, fnt,...) fnt.dispose(). or DrawString(myText, New...
3
by: goodmen | last post by:
I think the boost::concept_check is too complex. Here is my solution about static interface and concept. The static interface can be used as function param. It is just a proxy of the real...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
8
by: anonymous | last post by:
I'm having some trouble understanding the meaning of visibility. <?php class foobar { private $key1 = "cat"; private $key2 = "apple"; protected $key3 = "book"; public $key4 = 42; }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.