Re: Why Java interface?

From: Mary Wenge (mary_wenge_at_yahoo.ca)
Date: 12/27/03


Date: 27 Dec 2003 06:58:40 -0800

Thanks to everybody!

In C++, there are multiple inheritances; but in Java, a class can
inherit only one super class. In almost every Java book, interface is
introduced in just for the (similar) multiple inheritance purpose. But
I think the reason why it's useful in C++ to use multiple inheritance
is that we can reuse the **implementation** of those useful methods in
the super class. For example, if we want a new class FlyingCar, we can
inherit from classes Plane and Car, we don't need to care about how to
implement the complex method fly() in Plane, we just use it in
FlyingCar. We can feel the benefit of using inheritance. But if in
Java I define a class MyCar extends Car implements Flyable, there is
only an abstract method definition fly() in interface Flyable, I have
to implement it in my MyCar class, why do I still **inherit** it?
That's why I posted my original question.

Polymorphism is useful in design in C++. For example, if we want to
draw figures Point, Line and Circle, traditionally we can:

void fdraw (void *P)
{
  switch(p->objectType)
  {
      case Point:
          (Point *)p->draw();
           break;
      case Line:
          (Line *)p->draw();
           break;
      case Circle:
          (Circle *)p->draw();
           break;
      default: error();
  }
}

There is a disadvantage: if we want to add a new figure Rectangle, we
have to update our fdraw() by adding:

      ......
      case Rectangle:
          (Rectangle *)p->draw();
           break;
      ......

By using virtual function, it will be better. We define an abstract
super class Figure with a virtual function:

      virtual void draw() = 0;

All the detailed figure classes, Point, Line, etc, inherit from it and
implement the method draw(), and then define a unique calling form:

void fdraw(Figure *p)
{
    p->draw();
}

Now if we want to draw a figure, no matter what it is, we just use the
unique calling interface fdraw(), as below:

   Point p(200,300);
   fdraw(p);
   Line l(100,200,300,400);
   fdraw(l);
   ......

So, in my opinion, maybe the most useful feature of Java interface is
the polymorphism, or the **type**. It is not used to let you share the
implementation code in the method of a super class, it might be useful
for the derived classes to share the unique calling form of the
methods whose prototypes are defined in the super interface. So, if an
interface has only one, never another, derived class, then it seems
there is no necessity to use interface. Java interfaces are useful for
many classes which will share some common features and make the
further use of those classes easier and clearer (in code and in design
pattern). Am I right? If yes, can anybody give an example to
illustrate this?

Are there any other advantages? (If you just give me the advantages in
1 or 2 theoretic sentences, it seems a little bit hard for me to
understand.:) Some simple examples are better.)

Thanks a lot.

Mary



Relevant Pages

  • Multiple Inheritance with Interfaces
    ... You cannot inherit from more than one super class in Java, ... according to all the texts 'Interfaces allow you to 'inherit' from ... But all an interface gives you is a method name ...
    (comp.lang.java.programmer)
  • Re: Multiple Inheritance with Interfaces
    ... > You cannot inherit from more than one super class in Java, ... > according to all the texts 'Interfaces allow you to 'inherit' from ... > earth does an Interface allow you to use methods from other ... void whatever ...
    (comp.lang.java.programmer)
  • Re: Darkroom software Help
    ... Apple's computer operating system OS-X includes the Java ... programing language, compiler, and integration software. ... have an Apple this fall. ... Said that, my system is based on a commercial lab interface, the ...
    (rec.photo.darkroom)
  • Re: A C++ Whishlist
    ... with the C++ string classes as compared with the Java ones. ... You can keep you structs entirely ... The simple separation of interface and implementation that header files ...
    (comp.lang.cpp)
  • Re: Interface question
    ... This is the beauty of interfaces: you don't inherit from something ... interface, and make all of the classes implement it. ... ListViewModel, which is the brains behind managing item selection, ... to make another control that could interact with any control that was ...
    (microsoft.public.dotnet.languages.csharp)

Loading