-1

what exactly does the super.onDraw() do in this example?

what if i leave it out, does it change what will happen when onDraw method is called?

 class Preview extends SurfaceView implements SurfaceHolder.Callback {

     // onDraw is a callback method of the SurfaceView class
     @Override
     protected void onDraw(Canvas canvas) {

     super.onDraw(canvas);

     // more code to implement some action when this method is called

     } // end onDraw method

 } // end Preview class

EDIT: i just found an excellent question and answer to help clarify the purpose of supercall, When NOT to call super() method when overriding?

Community
  • 1
  • 1
Kevik
  • 9,181
  • 19
  • 92
  • 148

3 Answers3

1

If you look at SurfaceView's source code, you see that it extends View and it doesn't override the onDraw() method, so it's View's onDraw() method that is being called.

Now look at View's onDraw() method:

/**
 * Implement this to do your drawing.
 *
 * @param canvas the canvas on which the background will be drawn
 */
protected void onDraw(Canvas canvas) {
}

It doesn't do anything. So calling super.onDraw() in your class will do nothing.

minipif
  • 4,756
  • 3
  • 30
  • 39
0

It calls the method onDraw() in the class SurfaceView.

Generally speaking, not in your specific case:

Since it is a base class there in the super class implementation, basic things like borders,colors, or whatever sets to the canvas object you passed.

If you don't want them write on your own here.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • so if it is calling the constructor or method in the parent class, SurfaceView, then how do i know when to call it or not? or is it safer to always call it in case i need that functionality? – Kevik Oct 03 '13 at 08:30
  • What does this means, weather constructor or method, you are telling that inherit the properties from my parent. And actually you need not to call like this explicitly, unless some special functionality(like setting some properties) on that object you are passing. – Suresh Atta Oct 03 '13 at 08:32
0

It does nothing ) SurfaceView's onDraw is View's onDraw and it's empty. But there is no guarantee that it always be empty. So you better call it if you do not have some reasons not to call.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • i think that is why in many code completion tools, they will add the super call automatically when you add the callback method – Kevik Oct 03 '13 at 08:42
  • by default you always should call super class method if you override it. Because if you not call it, you can break something. But in some cases, when you really sure, you can remove super's implementation totally if you dont need it (imho bad desing). – Leonidos Oct 03 '13 at 08:46