143

A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain.

For example, given the interface:

public interface IAnything {
  void m1();
  void m2();
  void m3();
}

the following abstract class gets merrily compiled without a warning or an error:

public abstract class AbstractThing implements IAnything {
  public void m1() {}
  public void m3() {}
}

Can you explain why?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
  • 5
    One cannot create an object of an abstract class. So, as long as an implementation is not provided for an abstract class, objects cannot be created for IAnything. So this is absolutely fine for compiler. Compiler expects that, any non-abstract class that implements IAnything must implement all the methods declared out of IAnything. And since one have to extend and implement AbstractThing to be able to create objects, compiler will throw an error, if that implementation do not implement the methods of IAnything left out by AbstractThing. – VanagaS Mar 19 '17 at 06:25
  • I had a concrete class that was extending its own "AbstractThing" in an identical scenario to this, and even though I hadn't implemented one of the methods in the interface, it was inexplicably compiling. *Now* it's doing what I expect it to, but I can't figure out what was causing it to succeed before. I suspect I hadn't `:w`'d one of the files. – Braden Best Mar 18 '19 at 19:04
  • you can see the anwer for a similar question https://stackoverflow.com/questions/8026580/an-abstract-class-in-java-need-not-implement-any-methods-from-its-implementing-i/60831527#60831527 – Vy Do Mar 24 '20 at 13:03

7 Answers7

177

That's because if a class is abstract, then by definition you are required to create subclasses of it to instantiate. The subclasses will be required (by the compiler) to implement any interface methods that the abstract class left out.

Following your example code, try making a subclass of AbstractThing without implementing the m2 method and see what errors the compiler gives you. It will force you to implement this method.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 2
    I think the compiler should still throw warnings regarding abstract classes that implement interfaces incompletely, simply because then you need to go looking through 2 class definitions rather than 1 to see what you need in a subclass. This is a language/compiler limitation though. – workmad3 Oct 13 '08 at 15:11
  • 3
    That wouldn't be a good idea, as there can typically be lots of abstract classes and the 'false' warnings would soon overwhelm you, causing you to miss the 'true' warnings. If you think about it, the 'abstract' keyword is there specifically to tell the compiler to supress warnings for that class. – belugabob Oct 13 '08 at 15:16
  • 4
    @workmad - if you have common implementations for a subset of interface methods it makes better sense to factor it into a separate base class (DRY trumps one-place-code) – Gishu Oct 13 '08 at 15:23
  • 4
    It would be dangerous to *require* you to put empty method implementations in an abstract class. If you did that then implementers of subclasses would inherit this non-behavior without the compiler telling them there's a problem. – Bill the Lizard Oct 13 '08 at 15:27
  • 11
    I think what workmad might be suggesting is that you define the methods in the abstract class without a method body and mark them abstract. Doesn't seem like a bad idea to me. – Dónal Oct 13 '08 at 23:32
35

Perfectly fine.
You can't instantiate abstract classes.. but abstract classes can be used to house common implementations for m1() and m3().
So if m2() implementation is different for each implementation but m1 and m3 are not. You could create different concrete IAnything implementations with just the different m2 implementation and derive from AbstractThing -- honoring the DRY principle. Validating if the interface is completely implemented for an abstract class is futile..

Update: Interestingly, I find that C# enforces this as a compile error. You are forced to copy the method signatures and prefix them with 'abstract public' in the abstract base class in this scenario.. (something new everyday:)

Gishu
  • 134,492
  • 47
  • 225
  • 308
7

That's fine. To understand the above, you have to understand the nature of abstract classes first. They are similar to interfaces in that respect. This is what Oracle say about this here.

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.

So you have to think about what happens when an interface extends another interface. For example ...

//Filename: Sports.java
public interface Sports
{
   public void setHomeTeam(String name);
   public void setVisitingTeam(String name);
}

//Filename: Football.java
public interface Football extends Sports
{
   public void homeTeamScored(int points);
   public void visitingTeamScored(int points);
   public void endOfQuarter(int quarter);
}

... as you can see, this also compiles perfectly fine. Simply because, just like an abstract class, an interface can NOT be instantiated. So, it is not required to explicitly mention the methods from its "parent". However, ALL the parent method signatures DO implicitly become a part of the extending interface or implementing abstract class. So, once a proper class (one that can be instantiated) extends the above, it WILL be required to ensure that every single abstract method is implemented.

Hope that helps... and Allahu 'alam !

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • That's an interesting point of view. It makes me think that "abstract classes" are really "concrete interfaces", i.e. interfaces with some concrete methods, rather than classes with some abstract methods. – Giulio Piancastelli Apr 09 '15 at 07:17
  • ... a bit of both really. But one thing for sure, they are not instantiable. – Grateful Apr 10 '15 at 09:15
6

Given the interface:

public interface IAnything {
  int i;
  void m1();
  void m2();
  void m3();
}

This is how Java actually sees it:

public interface IAnything {
  public static final int i;
  public abstract void m1();
  public abstract void m2();
  public abstract void m3();
}

So you can leave some (or all) of these abstract methods unimplemented, just as you would do in the case of abstract classes extending another abstract class.

When you implement an interface, the rule that all interface methods must be implemented in the derived class, applies only to concrete class implementation (i.e., which isn't abstract itself).

If you indeed plan on creating an abstract class out of it, then there is no rule that says you've to implement all the interface methods (note that in such a case it is mandatory to declare the derived class as abstract)

sharhp
  • 359
  • 3
  • 10
4

Interface means a class that has no implementation of its method, but with just declaration.
Other hand, abstract class is a class that can have implementation of some method along with some method with just declaration, no implementation.
When we implement an interface to an abstract class, its means that the abstract class inherited all the methods of the interface. As, it is not important to implement all the method in abstract class however it comes to abstract class (by inheritance too), so the abstract class can left some of the method in interface without implementation here. But, when this abstract class will inherited by some concrete class, they must have to implements all those unimplemented method there in abstract class.

3

When an Abstract Class Implements an Interface

In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract. For example,

abstract class X implements Y {   
    // implements all but one method of Y
}

class XX extends X {   
    // implements the remaining method in Y 
} 

In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.

Reference: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Vy Do
  • 46,709
  • 59
  • 215
  • 313
1

Abstract classes are not required to implement the methods. So even though it implements an interface, the abstract methods of the interface can remain abstract. If you try to implement an interface in a concrete class (i.e. not abstract) and you do not implement the abstract methods the compiler will tell you: Either implement the abstract methods or declare the class as abstract.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192