Wrox Programmer Forums
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 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 May 19th, 2009, 01:52 PM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Derived Interfaces

Hi All

I'm currently working through Professional C# 2008 book and I'm busy with the Derived interfaces part. I'm a programer, but I think my OO skills are a little lacking.

Anyway, working through the example in the book. I declared the following interface
Code:
public interface IBankAccount
{
 void PayIn(decimal amout);
 bool Withdraw(decimal amount);
 decimal Balance
 {
   get;
 }
}
Then I declared a derived interface from the previous interface
Code:
public interface ITransferBankAccount : IBankAccount
{
bool TransferTo(IBankAccount destination, decimal amount);
}
I then wrote two classes that uses these interfaces
Code:
public class SaverAccount : IBankAccount
{
// code here
}
 
public class CurrentAccount : ITransferBankAccount
{
// code here
}
Then in my main, it looks like this
Code:
class MainEntryPoint
{
 static void Main()
 {
   IBankAccount venusAccount = new SaverAccount();
   IBankAccount jupiterAccount = new CurrentAccount();
   venusAccount.PayIn(200);
   jupiterAccount.PayIn(500);
   Console.WriteLine(venusAccount.ToString());
   Console.WriteLine(jupiterAccount.ToString());
   Console.ReadLine();
 }
}
Now some of you will probably already have noticed that I've declared jupiterAccount as a IBankAccount type, rather than a ITransferBankAccount type, yet I've instanced it as a CurrentAccount object.

This isn't how the code in the book goes, but it was a mistake made by me, yet it compiles and works. When I explored this, it looks like I had access to all the methods/types defined in the IBankAccount interface, but even though the TransferTo() function is declared in the CurrentAccount class, I don't have access to it and calling it gives a compile error.

Is this intended?

Syn
 
Old October 31st, 2009, 05:36 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Yep thats would be correct, Your CurrentAccount has an "is a" relationship with IBankAccount meaning; a CurrentAccount is an IBankAcount. Since CurrentAccount is an IBankAccount at its lowest level of inheritance, an IBankAcount can contain a CurrentAccount but it will only expose the method signitures that it knows. However, an IBankAccount is not a CurrentAccount therefore it is invalid to assigment of an IBankAccount to a CurrentAccount is not valid either i.e.

IBankAccount saverAccount = new SaverAccount();
CurrentAccount myCurAccount = SaverAccount; // not valid becuase IBankAcount is not a CurrentAccount
// this is becuase the CurrentAccount designation would expose
// variables and method signitures than an IBankAccount does not have to offer.
__________________
Mike

Last edited by C@uark; October 31st, 2009 at 05:57 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Field debbiecoates Beginning VB 6 2 February 20th, 2008 06:58 AM
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 05:21 AM
Not a ReportDocument derived class chokk Crystal Reports 0 May 10th, 2006 08:21 AM
Force Derived Method rodmcleay C# 7 June 1st, 2005 01:19 AM
Using reflection to get derived classes jaucourt VB.NET 2002/2003 Basics 0 May 10th, 2004 09:14 AM





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