473,898 Members | 3,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(IFee Schedule feeSchedule);
}
class BusinessAccount : IAccount
{
void IAccount.PostIn terest()
{
// Code to post interest using the most favorable rate.
}

void IAccount.Deduct Fees(IFeeSchedu le 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 8587
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.goo gle.com...
Consider:
http://msdn.microsoft.com/library/de...ycomponent.asp

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

void IAccount.Deduct Fees(IFeeSchedu le 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.goo gle.com...
Consider:
http://msdn.microsoft.com/library/de...ycomponent.asp

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

void IAccount.Deduct Fees(IFeeSchedu le 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
implementati on 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
12233
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 written to a single interface, but support many different manufacturers' barcode scanning devices. I do not want to be tied to one manufacturers' software interfaces. - Must support use of the scanner from Visual Basic, and ideally from C/C++ and...
19
3236
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 pretty interesting.
7
2820
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; /*this is declaration*/
0
923
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... I create a template ASP.net webservice. In the helloworld function, I create an instance of a wrapperclass (wich wraps unmanaged C++), and call a function upon it, like this:
21
1751
by: Philipp | last post by:
Hey, did anyone have a good paper about the opject orianteted concept? wishes
21
1522
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 Font(...),...)
3
1706
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 implementation. And the Concept can be used when building the class hierarchy. It is almost a private inherit, but for the sake of providing an uniform look with the static interface version, the private member is used.
6
38549
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 through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
8
1208
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
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9840
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10483
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4706
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.