How do I extend a class that has parameters in its constructor?

How do I extend a class that has parameters in its constructor?
[77 byte] By [Virum] at [2007-9-27 4:42:12]
# 1

I ran into this problem on an assignment... I was making a clone of mspaint (a real generic clone to be sure) and I had different objects that you could draw (circle, square, ellipse, etc), so I wanted to create a shape class and then have individual classes (each shape) extend the generic shape class because every shape I used had the same basic variables (x1,y1,x2,y2). What happened was that I remade the variables in each subclass and it ran, just didn't work... When I remade the variables, they reset from the original... anyway, to make a long story short, they inherited the variables from the upper class shape.

I don't know if that helps you at all, but I'd think any class has a default constructor that takes 0 paramenters so you could just do the generic

public class myclass extends theirclass

couldn't you? If its your own class that you want to be extended, overload the constructor with one that takes no variables.

kev

SeversonK at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...
# 2

Supply a call to the super() constructor in your subclasses constructor.

This must be done on the first line of your constructor.

public class A {

private int is;

public A() {

System.out.println( "default" );

}

public A( int is ) {

this.is = is;

System.out.println( "is = " + this.is );

}

}

public class TestA extends A {

public TestA( int is ) {

super( is );

}

public static void main(String args[]) {

new TestA( 2 );

}

}

GumB. at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...
# 3

My super class is SpaceM; It takes a grphics configuration for contructor object. When I extend it normally I get an error saying something about a bad constructor.

Here is the error:

D:\J21WORK\My Games\Space\PaintMyShip.java:3: cannot resolve symbol

symbol : constructor SpaceM ()

location: class SpaceM

public class PaintMyShip extends SpaceM{

^

1 error

Process completed.

What do I do to fix, this I am not sure I understand you.

Virum at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...
# 4
Thanks GumB, I solved my problem. I just created an extra constructor inb my superclass with no parmeters and it works nicely. :) YOu want a duke or two?
Virum at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...
# 5

No I don't need those dukes, but check into the super() call to constructors. You may need that when you can't change the Base class. Another example of it, that we see all the time, is when we extend a JFrame. To get the title in the title bar, we often go like this:

super("My Title Bar Text");

Here, we are calling the JFrame constructor that takes a string as a parameter(from our subclass constructor). It comes in handy sometimes.

GumB. at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...
# 6
Okay, thanks. :)
Virum at 2007-7-5 14:28:56 > top of Java-index,Archived Forums,Java Programming...