Re: Questions of Designs

From: Falk Tannhäuser (falk.tannhauser_at_crf.canon.fr)
Date: 09/10/04


Date: Fri, 10 Sep 2004 18:38:35 +0200

Shashank wrote:
>
> Oh, I don't think am being anti C++ and neither being pro.
> ABC is still a class. In OOPS classes are written so that we may create their objects that will have some
> state and behavior.
>
> While language constructs allow us to create abstract class thats fine. But that is still a class, but we
> are not writing implementation code.
>
> But its not interface. Interface definition should not have option to write implementation code.

What is the advantage for the programmer of not being able to write
implementation code in an interface? There *is* an advantage for
the compiler implementer because it simplifies the implementation
of multiple inheritance - that is the reason why Java has different
keywords 'class' and 'interface', allowing to inherit from multiple
interfaces but at most one class.
However, from the programmer's point of view, the distinction seems
to be quite artificial and limiting to me - very often you want to
put pre-/postcondition checks or otherwise factor out common
behaviour into the ABC and Java interfaces don't let you do that.
C++ example:

class MyAbc
{
  virtual result_type do_foo(param_type) = 0; // private

public:
  result_type foo(param_type p) // Deliberately not virtual!
  {
    check_preconditions(p);
    result_type r = do_foo(p);
    check_postconditions(r);
    return r;
  }
};

class MyImplemOne : public MyAbc
{
  virtual result_type do_foo(param_type p)
  { /* Implementation ... */ }
};

class MyImplemTwo : public MyAbc
{
  virtual result_type do_foo(param_type p)
  { /* Another implementation ... */ }
};

Client code would only call foo() and Pre/Postcondition check
will always be performed no matter what derived class you have
to do with. (Yes, I know - in Eiffel, everything is much nicer...)
How would I do the same with a Java 'interface' at the place of
class MyAbc?

BTW, you can't create instances of C++ ABC either.

Falk



Relevant Pages

  • Re: Why ABCs make bad Interfaces
    ... > equivalent to an Abstract Base Class (ABC). ... > because it is common practice to implement an interface using ABC's. ... To convert an object to its interface, you return a fat pointer ... The NaiveInt constructor has to set up the vtables, ...
    (comp.object)
  • Re: Declaring a Constructor in an Interface?
    ... ABC: ... Interface: ... >> static void SpeedTestInterface ... > implementing IFoo. ...
    (microsoft.public.dotnet.languages.csharp)
  • Problems with extant SW for emailing
    ... Problem with Google Mail and Ubuntu ... example 1) "ABC List" in the subject line and if anybody on ABC in the ... an Internet-connected interface, then it can indeed receive mail. ... Modify settings or unsubscribe at: ...
    (Ubuntu)
  • Re: Problem with Google Mail and Ubuntu
    ... example 1) "ABC List" in the subject line and if anybody on ABC in the ... listens on port 25 and the interfaces you specify. ... an Internet-connected interface, then it can indeed receive mail. ... via the file system to the local mail ...
    (Ubuntu)
  • Why ABCs make bad Interfaces
    ... the differences between Abstract Base Classes (ABC) and Interfaces: ... because it is common practice to implement an interface using ABC's. ... to all of the neccessary vtables and lookups. ...
    (comp.object)