-1

I know it is a very basic, classic and simple question to be asking about differences between interfaces and abstract classes in java. But this question is a bit different.

I understand that Java does not support inheriting from multiple classes to avoid the diamond problem and hence came up with the concept of interface as an alternative way to facilitate a kind of multiple inheritance. But, is this the only reason why interfaces exist?

What if, assume for some reason, Java Guys in one of its new version decide to overcome this limitation and allow inheriting from multiple classes, then, what would be the difference between abstract classes and interfaces? Will they be simply synonymous? Will the interfaces still have a reason to exist or will simply be redundant?

Drona
  • 6,886
  • 1
  • 29
  • 35
  • You mean if classes where allowed to inherit from multiple *completely* abstract classes? (i.e. classes with *only* abstract methods) – aioobe May 14 '12 at 08:36
  • m afraid its a bit too late for java guys to do that as lot of code is already written with the old rules and changing it would cause a lot of compatibility issues... – Surender Thakran May 14 '12 at 08:38
  • You can't debate about the Concept of Abstract class and interface here. You can only ask the programmatical problems. And adding to this you can't say that the pure Abstract class is same as interface. eg. Can you inherit multiple class in abstract class? No. For that you must need to have interfaces. – Japan Trivedi May 14 '12 at 08:52
  • @JapanTrivedi I am not debating on the current state of affairs in Java today. But rather trying to understand that had Java allowed inheriting from multiple classes, would it have had interfaces as entities in its language specification. – Drona May 14 '12 at 09:00
  • 1
    @VikasNalwar: If it had had multiple inheritance from day 1, quite possibly not, since abstract classes would have done the job. – T.J. Crowder May 14 '12 at 09:04
  • @T.J. Crowder Thanks for the straight answer :) – Drona May 14 '12 at 09:06

4 Answers4

3

A class is "abstract" if it has even one abstract method; other methods can be implemented in the abstract class. Interfaces can't do that; they're purely abstract.

public interface Iface {
    void foo() {
        System.out.println("Foo");
    }
}
$ javac Iface.java 
Iface.java:2: interface methods cannot have body
    void foo() {
               ^
1 error

But if you're talking about a completely abstract class — where all of the methods are abstract — then if you posit multiple inheritance I'm not immediately seeing any real remaining differences, no.


Re your comment on your question above:

But rather trying to understand that had Java allowed inheriting from multiple classes, would it have had interfaces as entities in its language specification.

If Java had had multiple inheritance from Day 1, quite possibly not, since abstract classes would have done the job.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    But, I can have a pure abstract class with all abstract methods behave quite well like an interface, then why have interfaces at all? – Drona May 14 '12 at 08:42
  • @VikasNalwar: There's the conceptual difference, but more importantly for Java as it really is (single inheritance), having `interface` allowed us to have multiple inheritance "lite" by implementing multiple interfaces. – T.J. Crowder May 14 '12 at 08:45
  • 1
    If `Interface1` which has a property `Foo` is inherited by interfaces `Derived1` and `Derived2`, both of which are inherited by `Subderived1`, then `Subderived1` will only have a single method `Foo` that it can override, which will be shared by all four interfaces. With abstract classes, things could be murkier. – supercat May 15 '12 at 16:48
  • 1
    @supercat: Yeah, depending on how it was done. This is all *highly* speculative, but I figure it would be okay as long as the signatures were the same and there was at most *one* concrete implementation (so `super` didn't have to be multiple choice). – T.J. Crowder May 15 '12 at 17:39
  • @T.J.Crowder: Ambiguity would arise if one of the classes which is inherited overrides the method (to have the same flexibility of interfaces, classes should be able to inherit multiple purely abstract classes in addition to one that isn't). If the class which derived both that concrete class and a sub-derived abstract class also overrode that method, things could get, at minimum, rather confusing, unless the inheritance declaration noted which class should be considered non-abstract (in which case, one is essentially back to having interfaces). – supercat May 15 '12 at 17:54
  • @supercat: Right. I think I was clear about how it could get messy. – T.J. Crowder May 15 '12 at 22:01
2

Will they be simply synonymous? Will the interfaces still have a reason to exist or will simply be redundant?

(Since you seem to be aware of the fact that comparing abstract classes with interfaces is a common question, I'll assume you know that interfaces can't have state nor default method implementations.)

A class with only abstract methods is just the same as an interface, so yes, in a sense interfaces would just be syntactic sugar I guess.

In code,

interface Foo {
    void method();
}

class MyClass extends MyBaseClass implements Foo {
    public void method() { ... }
}

would hypothetically speaking be equivalent to

abstract class Foo {
    public abstract void method();
}

class MyClass extends MyBaseClass, Foo {
    public void method() { ... }
}

Related question (almost a dup!):

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I seem to perfectly agree with your logical reasoning. So, we would not need interfaces at all. – Drona May 14 '12 at 08:49
  • That's right, but we could probably do without abstract classes as well! :-) The reason why they where incorporated in the language in the first place is probably because they provide a conceptually clear separation between implementation and interface. – aioobe May 14 '12 at 08:52
  • Interface methods are always public. But ok, we can say that is just an oddity and say that's of no use either. – dube May 14 '12 at 09:48
  • Right. The abstract classes (when used as interfaces) must have only public methods :-) Good point. – aioobe May 14 '12 at 10:09
  • Actually I think this it is a stupid rule. Why is package-private not allowed? They probably just didn't think about that when Java was made :) – dube May 14 '12 at 10:10
  • @dube, that's interesting. I'm not so sure it would make sense. What if I implement the interface in a different package? Then package-private means something quite different from what the interface-writer may have thought of. – aioobe May 14 '12 at 10:13
0

Still I think there is a difference.Interface is a contract only by which implementation belongs to you, even if limitation to multiple inheritance is removed.In java, you can derive multiple inheritance behavior by using interfaces, but that may not bring all the features of real multiple inheritance(Like calling its super classes etc)It is a kind of technique only.

An abstract class is a partial object entity, may contain full implementations as well.So even if it is possible to extend multiple classes, what you do with interface is different.I think multiple inheritance and abstract class and Interfaces are different in Java.

UVM
  • 9,776
  • 6
  • 41
  • 66
  • But dont you think, we could enforce the contract by way of pure classes as well instead of interfaces? – Drona May 14 '12 at 08:46
  • In that case in terms of java terminology, we will not call it as a contract, we are "over-riding" it or changing the existing behaviour.Is it not it? – UVM May 14 '12 at 09:07
  • But, we would not have any implementation of behaviour in a pure abstract class just as the case is with interfaces today. So, we would not be overridng anything. Just like the way it is in C++; we can enforce the contract through pure abstract classes. – Drona May 14 '12 at 09:11
  • what about a header(.h) is doing in C++? can we co-relate to an interface? Mostly a header file looks like a interface where you can see only method declaration.Also, in C++, there is an abstract class.Also in C++, there is a MULTIPLE inheritances.Do you think are there any differences between these? – UVM May 14 '12 at 09:13
0

An interface already IS a special abstract class type which only allows public abstract methods in it.

It is a pattern which forces us to abide by the no-diamonds-allowed contract.

So if you make a compiler which can check abstract classes for not having any implementations on it, you could replace all interfaces with abstract classes (which abide by this contract)

dube
  • 4,898
  • 2
  • 23
  • 41