いちにち
いちにち

Reputation: 417

Interfaces and dynamic method dispatch

Normal overriding (without the use of interfaces)

class A {
    int x = 0;
    void show() {
        System.out.println("In A. x : " + x);
    }
}

class B extends A {
    void show() {
        System.out.println("In B. x : " + x);
    }
}

class C extends A {
    void show() {
        System.out.println("In C. x : " + x);
    }
}

class Test {
    public static void main(String args[]){
        A obj = new B();
        obj.show();
        obj = new C();
        obj.show();
    }
}

This is the same as doing that with interfaces:

interface A {
    int x = 0;
    void show();
}

class B implements A {
    public void show() {
        System.out.println("In B. x : " + x);
    }
}

class C implements A {
    public void show() {
        System.out.println("In C. x : " + x);
    }
}

class Test {
    public static void main(String args[]){
        A obj = new B();
        obj.show();
        obj = new C();
        obj.show();
    }
}

Output in both the cases will be the same and even the implementation is similar. My question is, why have interfaces when you can do the same thing by using dynamic method dispatch?

Upvotes: 1

Views: 3829

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 148870

Interfaces and superclasses are for different use cases :

  • superclasses allow to factorize common methods (but in java a class has only one parent class)
  • interfaces declares constants and/or method signatures, but a class may implements as many methods it needs

Interfaces are necessary when proxying, because a proxy can only implements interface and does not extends classes. And proxies are heavily used in a framework like Spring to have a simple Aspect Oriented Programming.

Upvotes: 1

Yagnesh Agola
Yagnesh Agola

Reputation: 4639

There are some points to consider for this :

  1. Interface give you option to your class to extends with other class.As you know, you can not extend multiple class but you can implement multiple interface (to achieve multiple inheritance) .
  2. By extending class you can not force sub-class to override method define in the super-class.While in Implementation of interface force to implement your method in sub-class.
  3. It is depend on the implementation that which will be your need. You want sub-class to must implement method than you need to use interface design, while you will not bother about sub-class override super-class method or not than you can used extends design.

I hope this will make you clear difference about both design.

Upvotes: 2

André Stannek
André Stannek

Reputation: 7863

Vikingsteve is right but I'll add a little more detail.

There is a difference between type inheritance and implementation inheritance. In Java you can have multiple type inheritance but not multiple implementation inheritance.

This means a class can implement multiple interface and inherits the type from each of those interfaces. An object created from that class is basically of all those types at once. But you can only extend one other class and inherit its implementation.

Upvotes: 1

Related Questions