File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class inplementing an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class inplementing an interface" Watch "abstract class inplementing an interface" New topic

abstract class inplementing an interface

Yuva Raj Sah

Joined: Apr 28, 2004
Posts: 6
I am a bit confused with the following code
interface I1{
public int methodA(int a);
abstract class example implements I1{
//public int methodA(int a){
//return a;
public static int methodC(){
return 1;
public static void main(String[] args){
abstract void methodB(int a);
I thought the code should not compile as the abstract class is not implementing the interface method. But the code compiles and the output is 1.
Any explanation!!!
C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
Well, since the class is abstract, it is legal to leave the implementation of the interface's method to an extending concrete class.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
K&B page 133 states clearly "An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must)".
Inheriting (abstract) methods from an interface is no different than inheriting them from an abstract baseclass.
In either case you must either implement them and/or mark your own class as abstract (in case you wish to possibly defer implementation to your own subclasses).

Gauri Savanur

Joined: Apr 25, 2004
Posts: 9
The code seems to be perfectly legal.
Because if u are not implementing the method of the interface in the implementing class then u have to declare it abstract. Since the implementing class is declared as abstract it will compile.
The code will also run and give the output 1 because abstract class can be run as a standalone program. Just u can't create instance of it. Trying to create the instance will give u compilation error.
Yuva Raj Sah

Joined: Apr 28, 2004
Posts: 6
Thanks for the explanation
Yuvara Sah
I agree. Here's the link:
subject: abstract class inplementing an interface
It's not a secret anymore!