Inheritance override draw()


#1

I have a class using registerMethod(“draw”,this); Works fine.
In a subclass I am attempting to override the draw() method in the parent in order to ‘add’ to the original.
I cannot override, and I have tried super.draw(); Nothing is added from the subclass draw() but it does still draw from the parent.

  1. Am I attempting the impossible?
  2. If it is possible, do I have to repeat all the draw() code from the parent plus the additional items for the child, I wanted to avoid repeating and it still did not work anyway?
    Some guidance please.

#2

Can you please post a simplified MCVE that shows what you’re trying to do?

Also you might check out these tips on asking questions:


#3

Thank you Kevin, The purpose was to test whether I could override the draw() method in a parent by only adding new items in a child rather than having to re-draw (in the child) the parent as well as the child extras.
It appeared not to be possible, however…

your comment focused my mind on providing an example which ended up curing the problem.
I had made the simple mistake of making two rectangles the same size on top of each other, which masked the intended result. :blush:
In fact it was possible, as you will know anyway, but I did not.

I had better add the example here lest I risk my other hand being slapped :laughing:
Take the comment marks off the line with super.draw()

Panel pan;

void setup(){
  size(500,500);
  pan = new Panel(150,150,150,150,color(0,255,0));
  pan.pane.labClr=color(255,255,0);
  pan.setCaption("Green",color(255,0,0));
  pan.pane.setCaption("Yellow",color(255,0,0));
}

void draw(){
  background(0);
}

//===============================================================
public class Pane {
  int x, y, w, h;       
  int labClr, capClr;
  String caption="";  
    
  Pane(int _x, int _y, int _w, int _h,int _labClr) {
    x = _x; y = _y; w = _w; h = _h; labClr = _labClr;
    registerMethod("draw",this);
  }
//-------------------------------------------------------------- 
  void setCaption(String _caption, int _capClr) {
    caption=_caption; capClr=_capClr;
  }
//-------------------------------------------------------------- 
  void draw() {
      fill(labClr);
      noStroke();
      rect(x, y, w, h);
      textAlign(RIGHT,CENTER);
      fill(capClr);
      text(caption,x+w/2,y+h/2);
  }//draw
}
//===============================================================
public class Panel extends Pane {
Pane pane;

  Panel(int _x, int _y, int _w, int _h,int _labClr){
    super(_x, _y, _w, _h, _labClr);   
    pane = new Pane(x+70,y+70,w-70,h-70,labClr);
  } 
  
  void draw(){
//    super.draw();
    stroke(255);
    line(x+30,y+30,x+30,y+230);
    line(100,100,x+250,y+150);
  }
}

#4

That’s why we ask to see an example! :slight_smile:

Taking a step back, I wonder why you’re using the registerMethod() function for this? It seems like you should probably just call one function from the other:

// sketch-level draw function
void draw(){
  pan.draw();
}

It also feels a little weird to need a reference to the parent class inside the child class. Why do you need this setup?


#5

Thank you for you reply Kevin. I attempted reply to the notification email but it was stopped because the forum does not recognise gmail, only googlemail, despite them being the same.

I am heartened by your use of the term ‘little weird’ instead of ‘big weird’.
In answer to your points…

This was a concocted minimised example to solve one issue in a larger situation. Pane and panel are a inheritance chain. In the actual (not the example of principle) the pane class is inherited by other classes. Panel inherits more than one pane of different size and position and also inherits from other classes such as buttons and titlebar. It works (now).

By encapsulating the objects I am able to make the whole chain invisible or not (with if(visible) in each draw() ) as a single entity, rather than having to hide each individually with external code.

Without the call to super the parent draw is not executed but instead fully overriden by the child, as illustrated by removing the super call. You suggest it is weird to use super, but not in the oop books that I have read.
One aim is the self drawing of objects (registerMethod() ) instead of cluttering the main draw() with a long list of objects and code.

I actually fail to see the gain in pan.draw() over super.draw() but the disadvantages are what caused me to take this route and it is not clear from your post where the void draw() is; whether it is the main draw() or is it in the panel, (a fuller example might be clearer :slight_smile: ). If it is the main then it defeats my aim of encapsulation; in the same way each of my classes has its own mousePressed() method accessed by a simple one line external call.

All detail is encapsulated within each class and is thus (to me) less confusing than an external string of code for all mousePressed() events in one place. The encapsulation thus also aids debugging.

If I am wrong, please advise with reasoning so that I may improve my code and/or encapsulation methodology.

Kevin, as an addendum, I also use within Panel class void setPanePosition(x,y …) and then use x + super.x to make the external call parameters of pane relative to the panel and not the world; if this makes sense. That is also super within the child.


#6

You have a better idea of your context, so if this make sense to you, then you should stick with it. After all, a lot of programming is subjective, so go with whatever fits in your brain the best!

But to hopefully explain what I meant, here’s a different example:

class Animal{

  Animal(){
    registerMethod("draw", this);
  }

  void draw(){
    println("hello I'm an animal");
  }
}

class Cat extends Animal{
  Animal animal;

  void draw(){
    super.draw();
    println("meow");
  }
}

In this example, it’s not clear to me why Cat needs to both extend Animal and contain an Animal variable. It’s also not clear to me why I should use registerMethod() instead of doing something like this:

Animal myAnimal = new Animal(); //could also be new Cat()

void draw(){
  myAnimal.draw();
}

But like I said, if your setup makes sense for your context, then feel free to ignore me!


#7

Thank you Kevin. I would certainly not ignore anybody’s input. I actually agree with you regarding Animal animal in Cat class, in my case I think you refer to Pane pane; which actually is an oversight and should work the same without it because it is inherited anyway. As for the rest, I am comfortable encapsulating and keeping the main draw() clear and so forth.
Thank you for your time and observations.