Eclipse, How to write subclasses that extends another class/NOC5_10(Particle extends VerletParticle)

I’m starting with Eclipse and also with the toxiclibs library. And I am a bit confused about how to work with “subclasses” that extends another class.

I’m trying to translate to Eclipse some examples of Shiffman’s book “Nature of Code”. For example “NOC_5_10_SimpleSpring.pde”. So I create a parent class (as the example sketch) that has a child-class “Particle” that “extends VerletParticle2D”.

I’m writing this subclass “Particle” like this:

    import processing.core.PApplet;
    import toxi.geom.Vec3D;
    import toxi.physics.VerletParticle;

    public class Particle extends VerletParticle {  
        PApplet p5;
        Vec3D loc;

        Particle( PApplet _p5, Vec3D _loc) {
            p5 = _p5;
            loc = _loc;
        }
    }

And I get this error:_ “Implicit super constructor VerletParticle() is undefined. Must explicitly invoke another constructor”_ and I don’t know exactly what this means.

In Toxiclibs library the class VerletParticle extends Vec3D, so is it possible that the new class “Particle” must have a constructor identically as a Vec3D constructor? Is it necessary? In this case, how to write in Eclipse a subclass that also extends another class?

Thank you for your help!

Tagged:

Answers

  • Answer ✓

    The Particle constructor must call super(...) where you replace the ... with the actual parameter names expected by VerletParticle.

    The message just says that VerletParticle doesn't have a parameterless constructor, so you must call explicitly one of its defined constructors.

  • edited March 2015 Answer ✓

    As an alternative, instead of extending VerletParticle, declare and instantiate it 1 inside your class.
    Just like you did w/ PApplet & Vec3D instances. Check it out: *-:)

    import processing.core.PApplet;
    import toxi.geom.Vec3D;
    import toxi.physics.VerletParticle;
    
    public class Particle { 
      final PApplet p5;
      final Vec3D loc = new Vec3D();
      final VerletParticle vp;
    
      Particle(PApplet pa, Vec3D pos) {
        p5 = pa;
        vp = new VerletParticle(loc.set(pos));
      }
    }
    
  • edited March 2015 Answer ✓

    Shorter version extending VerletParticle: :D

    import processing.core.PApplet;
    import toxi.geom.Vec3D;
    import toxi.physics.VerletParticle;
    
    public class Particle extends VerletParticle { 
      final PApplet p5;
    
      Particle(PApplet pa, Vec3D pos) {
        super(pos);
        p5 = pa;
      }
    }
    
    void setup() {
      Particle p = new Particle(this, new Vec3D(TAU, EPSILON, THIRD_PI));
      println(p, p.p5);
      exit();
    }
    
  • Answer ✓

    @GoToLoop==

    but in this type of case what is the interest of extending??? Could not be more simple && quick to write:

        p1 = new (VerletParticle2D(100,70));
        p2 = .......
    

    and add some display method with pos parameters???

  • @GoToLoop and @PhiLho, you help me so much!! Now I clearly see how I needed to call the "super(loc)". @GoToLoop: It´s very helpful to watch different alternatives and compare them. The best way to learn. @Akenaton: It is the way that Shiffman used to define classes (nodes, particles,...) in his book. Classes that extends VerletParticles with a display method costumized. thanks!!

Sign In or Register to comment.