Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending abstract class

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newbie question:
abstract class Shape
{
abstract void draw();
}
abstract class TwoDShape extends Shape
{
}
I want to be sure that TwoDShape doesnt have to override abstract method of superclass. However, if TwoDShape were not declared abstract then it must provide implementation otherwise a compiler error will ensue. I hope I'm correct on this, just need confirmation. TIA
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul!
You are correct. There are (at least) two options in your case:
a) declare the subclass abstract.
Then you do not have to implement the abstract method in the base classs
b) implement the method in the subclass

Then then class can be concrete (w.o. "abstract"), that is you can make instances of it via "new".
Hope that helps.
Greetings from Hamburg,
Stefan
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen your way too quick for me, I appreciate that. So you are able to implement the draw() with just {} and nothing in the body?
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul!
Implementing a method is just to add a body to the signature given in the interface or the abstract class.
In the most simple case, the body is empty and the method does nothing. The compiler will not complain, but in a realistic scenario, you would add some statements in between the { ... } to have some behaviour
I'm not sure, whether this is the answer to your question
Greetings, Stefan
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
absolutely Stephan, I've seen this done before when implementing some interface but not using those methods. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic