• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract class inplementing an interface

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation
Yuvara Sah
reply
    Bookmark Topic Watch Topic
  • New Topic