This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic