Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 23rd, 2006, 11:58 AM
Registered User
 
Join Date: Nov 2006
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

 
Old November 24th, 2006, 03:35 AM
Friend of Wrox
 
Join Date: Jun 2003
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




 
Old November 29th, 2006, 12:53 PM
Authorized User
 
Join Date: Sep 2006
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.




 
Old November 29th, 2006, 12:54 PM
Authorized User
 
Join Date: Sep 2006
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.


 
Old November 30th, 2006, 07:37 AM
Registered User
 
Join Date: Nov 2006
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.






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.