wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class inplementing an interface" Watch "abstract class inplementing an interface" New topic
Author

abstract class inplementing an interface

Yuva Raj Sah
Greenhorn

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){
System.out.println(methodC());
}
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
Hi..
Well, since the class is abstract, it is legal to leave the implementation of the interface's method to an extending concrete class.
Nimo.
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).


42
Gauri Savanur
Greenhorn

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
Greenhorn

Joined: Apr 28, 2004
Posts: 6
Thanks for the explanation
Yuvara Sah
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: abstract class inplementing an interface
 
Similar Threads
Another puzzling question
method override question
Multiple interfaces
What is the difference between these methods?