• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you implement an abstract method with an empty implementation?

 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is it possible?

You extend an interface/abstract, so you need to have to implement its methods. But what if you just changed the semicolon to curly braces in the subclass, and there's nothing in between the curly braces. Is it possible/legal?
 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yin Stadfield wrote:Hi, is it possible?

You extend an interface/abstract, so you need to have to implement its methods. But what if you just changed the semicolon to curly braces in the subclass, and there's nothing in between the curly braces. Is it possible/legal?



And what happened what you tried it? Also, don't forget to try it for methods that are not declared as void too.

Henry
 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Yin Stadfield wrote:Hi, is it possible?

You extend an interface/abstract, so you need to have to implement its methods. But what if you just changed the semicolon to curly braces in the subclass, and there's nothing in between the curly braces. Is it possible/legal?



And what happened what you tried it? Also, don't forget to try it for methods that are not declared as void too.

Henry


I just tried it now. Works fine for method that are declared as void and just as long as the implementation is marked as public. And empty method implementation won't work for methods with a return type, since the compiler requires a return statement. Thanks, Henry!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic