Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 23rd, 2006, 11:58 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to simsdagr8
Default Instantiating Interfaces

Hello World!

It's been a couple of weeks since I started learning C#. I have a question for all you braniacs about there.
The Professional C# 2005 book states that interfaces cannot be instantiated because they are like 'contracts' which contain method signatures. Fair point.

Now suppose you have two classes VenusBank and JupiterBank which both implement an interface called IBankAccount; how can it be legal to go ahead and have the following:

IBankAccount account1 = new VenusBank();
IBankAccount account2 = new JupiterBank();

Your help and comments would be highly appreciated.

Sims

  #2 (permalink)  
Old November 24th, 2006, 03:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Sims,

Your code isn't intantiating an IBankAccount object. Interfaces can't be instantiated because they are abstract (incomplete) types. Your code is instantiating a VenusBank object as an IBankAccount type, and a JupiterBank object as an IBankAccount type. When a type implements interfaces, it allows its objects to be treated as any of its interface types. Using
Code:
VenusBank account1 = new VenusBank()
you can call any method defined in VenusBank or IBankAccount. Using
Code:
IBankAccount account1 = new VenusBank()
you can only call methods defined in IBankAccount. The helpful formula for me is: interfaces let you treat a single object as though it we're of different types.

The advantage of programming to interfaces in this way, is that your client code doesn’t have to know anything about the specific type of object it is using (i.e., the VenusBank or JupiterBank object), and it doesn’t have to know anything about the classes that implement those objects either (i.e., the VenusBank or JupiterBank class). This enables you to reduce “implementation dependencies” (or high coupling) between your client and your apps other components, making your app less "fragile" as a result. The VenusBank and JupiterBank class can change their properties and methods over time, but since they implement IBankAccount, you are guaranteed that you can always treat a VenusBank or a JupiterBank object as an IBankAccount type without breaking your app. Also, you might want to extend your app to use additional bank objects, like PlutoBank and UranusBank. All you would have to do is implement a factory method that returns an IBankAccount and you could obtain any type of account object you like without ever having to change your account object creation code.

HTH,

Bob




  #3 (permalink)  
Old November 29th, 2006, 12:53 PM
Authorized User
 
Join Date: Sep 2006
Location: , , India.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , keep in mind . Only when we use the "new" keyword . Its said to be instantiated. The interfaces and abstract classes can be created (using 'new'). it can act as variable to store the references of creatable objects.




  #4 (permalink)  
Old November 29th, 2006, 12:54 PM
Authorized User
 
Join Date: Sep 2006
Location: , , India.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , keep in mind . Only when we use the "new" keyword . Its said to be instantiated. The interfaces and abstract classes cannot be created (using 'new'). it can act as variable to store the references of creatable objects.


  #5 (permalink)  
Old November 30th, 2006, 07:37 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to simsdagr8
Default

Thanks fror all your comments. That did help a great deal.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 05:21 AM
SQL Role Provider not instantiating jsqrd ASP.NET 2.0 Basics 0 March 26th, 2007 05:24 PM
Instantiating COM interop classes planoie VS.NET 2002/2003 0 September 30th, 2003 01:47 PM
Declaring and instantiating... jacob ASP.NET 1.0 and 1.1 Basics 4 August 21st, 2003 10:00 AM



All times are GMT -4. The time now is 10:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.