2

I want to do something like this in Java:

Superclass:

public class mysupclass {

    public mysupclass(int x, int y) {
       // code here
    }
}

Subclass:

public class mysubclass extends mysupclass {

    public mysubclass (mysupclass a, mysupclass b) {
        // code here
    }
}

How can I define mysupclass as arguments for my subclass constructor? Do I have to use super in the subclass constructor and what should I put in super?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Spyros
  • 313
  • 4
  • 12

7 Answers7

1

You'll definitely need to use super to call the superclass constructor - but what arguments you want to use are up to you. We have no idea what the classes are meant to do.

The fact that the subclass constructor has parameters of the superclass type is basically irrelevant as far as Java is concerned - it's just a coincidence. You need to follow all the normal rules of Java, which in this case means chaining to a superclass constructor. As you don't have a parameterless constructor in the superclass, you have to do that explicitly, providing values for x and y.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi and thanks for the answer. Any tips of what should I use as super? public mysubclass (mysupclass a, mysupclass b) { super(a,b); } it will not work :( – Spyros Feb 05 '12 at 07:56
  • 1
    @Spyros: Well no, because the superconstructor is declared to accept two `int` values, and you're trying to pass in two `mysupclass` values. We have no idea what these classes (or the constructor parameters in either class) are meant to represent, so there's no way I can suggest values. – Jon Skeet Feb 05 '12 at 07:57
  • Thanks again for responding. If mysupclass is a pair of int, then mysubclass is 2 pers of int, so a and b could be s'th like (0,0) or (1,2) etc. Does this helps? – Spyros Feb 05 '12 at 08:10
  • 1
    @Spyros: No, it doesn't - because "2 pairs of ints" *aren't* "a pair of int" - so your inheritance hierarchy would be inappropriate. Are you even sure you *should* have this inheritance relationship? Can you *really* think about an instance of your subclass as an instance of your superclass? – Jon Skeet Feb 05 '12 at 08:19
  • Well, yes. What we want to do with this is: superclass takes 2 ints to specify a point. subclass takes 2 superclasses (2 points) to specify a rectangle. – Spyros Feb 05 '12 at 08:22
  • 1
    @Spyros: That *definitely* doesn't sound like a good subclass relationship. A rectangle *isn't* a point. Instead, a rectangle can *compose* two points. – Jon Skeet Feb 05 '12 at 08:28
  • Maybe I wasn't specific enough. The 2 points of the subclass should be the upper-left and bottom-right points of the rectangle. Also it's not up to me. I'm asked to do it like this. :) – Spyros Feb 05 '12 at 08:30
  • @Spyros: You were specific enough, but this is still a *terrible* place to apply inheritance, and you should tell whoever asked you to do this that it's *simply a bad idea*. A point is *not* a rectangle. Are you *sure* they specifically said to use inheritance? If this is an academic exercise and you've represented it accurately, it raises serious questions about the competence of the person asking you to do this. – Jon Skeet Feb 05 '12 at 12:39
  • Hi Jon. Yes it is for an "essay". So it's s'th like a demostration of knowledge. I found a way to make it work. `public mysubclass (supclass u, supclass d) { super(u.getX(), u.getY());` ... and the I get d.getX() and d.getY() . getX() and getY() are public methods on the superclass. Ofcourse it doesn't mean you should do it in "production". – Spyros Feb 06 '12 at 18:13
  • @Spyros: You shouldn't do it ever, at all. It's just as meaningful to pass up `Integer.MAX_VALUE` and `Integer.MIN_VALUE` if it's not meant to actually do anything useful... – Jon Skeet Feb 06 '12 at 18:29
0

Reference for you: how to inherit Constructor from super class to sub class

Solution below:

Superclass:

public class mysupclass {

    public mysupclass(int x, int y) {
       // code here
    }
}

Subclass:

public class mysubclass extends mysupclass {

    public mysubclass (mysupclass a, mysupclass b) {
         //call to constructor of superclass
         mysupclass(a,b);
        // code here
    }
}
Community
  • 1
  • 1
0

Here you are an example of how that could be done:

Superclass (Point2D.java):

package test_superArg;

public class Point2D {
    private float x;
    private float y;

    public Point2D(float x, float y) {
        this.x = x;
        this.y = y;
        System.out.println("Parent class constructor:");
        System.out.println("X = " + this.x + ", Y = " + this.y);
    }
    public float getX() {return x;}
    public float getY() {return y;}
    public void setX(float x) {this.x = x;}
    public void setY(float y) {this.y = y;}
}

Subclass (Point3D.java):

package test_superArg;

public class Point3D extends Point2D {
    private float z;

    public Point3D(Point2D point2d, float z) {
        super(point2d.getX(), point2d.getY());  // must be the 1st statement in a constructor
        System.out.println("After calling super()");
        this.z = z;
        System.out.println("Child class constructor:");
        System.out.println("X = " + getX() + ", Y = " + getY() + ", Z = " + this.z);
    }
    public float getZ() {return z;}
}

Main class (Start.java):

package test_superArg;

public class Start {

    public static void main(String[] args) {
        System.out.println("Creating parent type class object...");
        Point2D point2d = new Point2D(2.0f, 4.0f);
        System.out.println("Creating child type class object...");
        Point3D point3d = new Point3D(point2d, 8.0f);
        System.out.println("Changing child class type object...");
        point3d.setX(10.0f);
        point3d.setY(20.0f);
        System.out.println("Parent type class object:");
        System.out.println("X = " + point2d.getX() + ", Y = " + point2d.getY());
        System.out.println("Child type class object:");
        System.out.print("X = " + point3d.getX() + ", Y = " + point3d.getY());
        System.out.println(", Z = " + point3d.getZ());
    }
}

Output:

Creating parent type class object...
Parent class constructor:
X = 2.0, Y = 4.0
Creating child type class object...
Parent class constructor:
X = 2.0, Y = 4.0
After calling super()
Child class constructor:
X = 2.0, Y = 4.0, Z = 8.0
Changing child class type object...
Parent type class object:
X = 2.0, Y = 4.0
Child type class object:
X = 10.0, Y = 20.0, Z = 8.0
Chupo_cro
  • 698
  • 1
  • 7
  • 21
0

As Jon Skeet already pointed out, you will have to use super in subclass constructor (Java compiler will force you to do it as long as there is no default constructor defined). What to pass as super parameters is up to you to decide.

I will assume that your mysupclass is a point and mysubclass is a rectangle. As default behavior, you can use first point as the one which will initialize mysupclass.

public class mysubclass extends mysupclass {
    public mysubclass (mysupclass a, mysupclass b) {
        super(a.getX(), a.getY());
        this.x2 = b.getX();
        this.y2 = b.getY();
        // anything else...
    }
}
Unobic
  • 47
  • 2
0

As a good general rule, the superclass should NEVER know about the subclass.

Consider redesigning your class hierarchy, or use an abstraction such as the subclass implementing an interface and the superclass expecting in instance of that interface.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • The superclass has *doesn't* know about the subclass - look at the text of mysupclass; it doesn't mention mysubclass. – Jon Skeet Feb 05 '12 at 07:39
  • Honestly Jon is right. I did not tell to anyone about the subclass :) Thanks for answering tho. – Spyros Feb 05 '12 at 08:11
0

Well i think u shud consider editing ur question and giving more details of what mysupclass represent in the context.As others have already explained u r trying to pass a mysupclass as argument to the mysupclass constuctor which is actually expecting an int value as parameter.Obviously if u r not passing a int value to the constructor of mysupclass the class cannot be instantiated as u r providing a parameterized constructor and it will not invoke a default constructor. Please specify more onto what ur requirement is? i mean what do u want to do by passing those mysupclass as arguments? because it wud be easier to set the variables u want to pass in the constructor using a super.variable=value; rather directly passing the mysupclass as argument.

Galaxin
  • 474
  • 2
  • 9
  • 21
-1

You may want to add a default constructor in as general coding practice says if you are overloading the default constructor then you should be adding it back in yourself. You may want to do some research into java constructors before you continue.

supclass :

public class mysupclass {

    public mysupclass(int x, int y) {
       // code here
       // Overloaded constructor.
    }
}

Subclass:

public class mysubclass extends mysupclass {

    public mysubclass () {
        super(mysupclass a, mysupclass b);
        // code here
    }
}
Popeye
  • 11,839
  • 9
  • 58
  • 91