1

I have this these classes;

public class Car extends JComponent {

}

public class Mazda extends Car {

}

public class Subaru extends Car {
}

In my car class I override the method paint component

    @Override
public void paintComponent(Graphics g) {
    //why my planets aren't painted by this method
    if (this instanceof Mazda) {
        g.fillOval(0, 0, this.getWidth(), this.getHeight());
        System.out.println(this.getClass());
    }
    if (this instanceof Subaru) {
        g.setColor(Color.blue);
        g.fillOval(0, 0, this.getWidth(), this.getHeight());
        System.out.println(this.getClass());
    }
}

It draws the instance of mazda just fine, but the code for instances of subaru just never gets called. It seems that subaru is not inheriting Jcomponent from Car? or why is not calling the painComponent? New to Java so I'm probably missing something basic

Gama
  • 352
  • 2
  • 16
  • 2
    why would you not override `paintComponent` in `Mazda` and `Subaru` ? – Scary Wombat Jan 26 '17 at 06:24
  • I tried but only the Mazda code worked and not in Subaru, i also tried making it abstract in Car but no luck. – Gama Jan 26 '17 at 06:26
  • how and where are the instances being added? – user85421 Jan 26 '17 at 06:27
  • 1
    Consider creating an [MCVE](https://stackoverflow.com/help/mcve). Remove extra code, add Mazda and Subaru on an empty frame and show us what exactly goes wrong. – default locale Jan 26 '17 at 06:32
  • @defaultlocale The problem is that only my Mazda cars are appearing on the screen, and the code inside of the if statement for Subaru never gets called. I set the location and size of the j components in their constructors – Gama Jan 26 '17 at 06:49
  • 1
    That information isn't nearly as helpful as the [mcve] that default locale requested. – Jon Skeet Jan 26 '17 at 07:08

2 Answers2

0

The Subaru class is surely inheriting from Car, but is is probably not being displayed at all. There are some reasons for that, but without seeing the code it is just guessing:

  1. Subaru not added to the parent, or substituted by another instance (Mazda?)
  2. the parent where it is added is not being displayed
  3. Subaru off screen, no need to paint it
  4. Subaru with zero dimension, nothing to paint
  5. ...

Note: the use of instanceof often indicates a flaw in OOP design: Why not use instanceof operator in OOP design?

It would be more OO if each subclass has its own version of paintComponent without having to use instanceofat all. One advantage doing so: the Car class does not need to be changed if adding a new car model.

Community
  • 1
  • 1
user85421
  • 28,957
  • 10
  • 64
  • 87
0

I think, you have a problem with design, because, if you want to @Override method from super class, good option is do it in base class like Mazda or Subaru,especially, you want to specified different behaviour. In abstract class like Car you can @Override method who is common for Mazda and Subaru, and is not important with child of super class do it this. So, I think You can write this structure like this:

class  Car extends JComponent{

}

class Mazda  extends Car{

  @Override
  public void paintComponents(Graphics g) {
    g.fillOval(0, 0, this.getWidth(), this.getHeight());
    System.out.println(this.getClass());
  }
}


class Subaru extends Car{

  @Override
  public void paintComponents(Graphics g) {
    g.setColor(Color.blue);
    g.fillOval(0, 0, this.getWidth(), this.getHeight());
    System.out.println(this.getClass());
  }

}

and then create class Mazda Mazda mazda = new Mazda() and call method: mazda.paintComponent(... or use polimorphism and create e.q. Mazda like this: Car mazda = new Mazda();

MatWdo
  • 1,610
  • 1
  • 13
  • 26