Re: Interfaces vs Classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Frank Rizzo (none_at_none.com)
Date: 02/24/05


Date: Thu, 24 Feb 2005 14:05:50 -0800

Thanks, this was helpful. However, my design doesn't fall into this
category.

Telmo Sampaio wrote:
> I wrote this last night... I dunno why it did not go thru...
>
> Hi Frank,
>
> It is normal to feel like that. The big advantage of using interfaces is
> that they extend the use of polymorphism beyond the class hierarchy. They
> are normally used to create small frameworks as well. Here are two examples:
>
> - imagine you are responsible for writing an ATM application for a bank.
> Thru your application you will allow users to Deposit, Withdraw and check
> their Balance for Checking accounts and Savings Accounts. No matter what
> kind of account your application handles, it needs to execute those tree
> operations. You can sure derive Checking and Savings from an abstract
> BankAccount that contain the necessary members. Now imagine that in the
> future, your ATM application may also be used to access Credit Card accounts
> and that the CreditCard class is not in the same inheritance hierarchy as
> the BankAccounts. You still need to force it to have the Deposit, Withdraw
> and Balance members. An interface is the perfect solution here.
>
> - another real example. You are probably aware of the Sort method for the
> Array class. It sorts data in a single dimension array. Remember that arrays
> can be of any type: integers, strings, Animals, BankAccounts, etc. The Sort
> method does not care about the data type. It access the underlying object
> thru the IComparable interface. Therefore, to make your classes sortable,
> you need to implement the IComparable interface. This has nothing to do with
> inheritance.
>
> I hope this helps a little bit.
>
> Telmo Sampaio
> MCT
>
> "Frank Rizzo" <none@none.com> wrote in message
> news:eybBWJhGFHA.2748@tk2msftngp13.phx.gbl...
>
>>We are having one of those religious debates at work: Interfaces vs
>>Classes. My take is that Classes give you more flexibility. You can
>>enforce a contract on the descendant classes by marking methods abstract
>>(which is all that an interface does). In addition, the classes allow you
>>to be flexible by adding functionality that child classes inherit. Or by
>>providing optional functionality via virtual methods.
>>
>>Now, I understand that Interfaces have its place, but I am having a hard
>>time finding a situation where the classes (via inheritance) do not
>>provide an identical or better solution.
>>
>>Am I missing something? I am open to arguments from both sides.
>>
>>Regards.
>
>
>



Relevant Pages

  • Re: Interfaces vs Classes
    ... their Balance for Checking accounts and Savings Accounts. ... and that the CreditCard class is not in the same inheritance hierarchy as ... An interface is the perfect solution here. ... It sorts data in a single dimension array. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interfaces vs Classes
    ... that they extend the use of polymorphism beyond the class hierarchy. ... their Balance for Checking accounts and Savings Accounts. ... and that the CreditCard class is not in the same inheritance hierarchy as ... An interface is the perfect solution here. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Tired of 100s of stupid Getter/Setter methods
    ... but interface does not hide the other methods of implementing ... telling you that you need to refactor a new common superclass. ... Again, composition instead of inheritance. ... A StringVector cannot take Integer parameters. ...
    (comp.lang.java.programmer)
  • Re: A taxonomy of types
    ... (I do not see why inheritance is required here.) ... without an »object-oriented programming language«, for example, ... in the programming language used. ... indirectly) implements an interface IA whose declaration ...
    (comp.lang.misc)
  • Re: Beginner, Which language
    ... > That's much more true for implementation inheritance than interface ... > compared to the run time support Java requires. ... > - All base classes of the interface must also be declared with interface ...
    (comp.programming)