Problem with Inheritance
I can't figure out what's wrong with my code are here, when I try to compile the Box class, the compiler tells me that "can't find the symbol-constructor Rectangle()
publicclass Rectangle
{
privatedouble length;
privatedouble width;
privatedouble ar;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
publicdouble area(double ar)
{
ar = length*width;
return ar;
}
publicdouble getLength()
{
return length;
}
publicdouble getWidth()
{
return width;
}
}
And
publicclass Boxextends Rectangle
{
privatedouble depth;
privatedouble vol;
public Box(double depth)
{
super();
this.depth=depth;
}
publicdouble area()
{
returnsuper(ar);
}
publicdouble volume (double vol)
{
vol=super(ar)*depth;
return vol;
}
}
[2816 byte] By [
Emotionsa] at [2007-11-27 9:59:40]

The error message is telling you exactly what's wrong.
Do you see constructor in the Rectangle class that doesn't take any arguments?
Constructor rules:
1) Every class has at least one ctor.
1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}
if you want one.
1.3) Constructors are not inherited.
2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)
or a call to another ctor of this class this(...)
2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()
as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.
The constructor of Rectangle takes in two arguments, which you are not passing. You can also make a default constructor without any arguments to solve the problem.
> The error message is telling you exactly what's
> wrong.
>
> Do you see constructor in the Rectangle class that
> doesn't take any arguments?
>
>
>
> Constructor rules:
>
> 1) Every class has at least one ctor.
>
> 1.1) If you do not define an explicit constructor for
> your class, the compiler provides a implicit
> constructor that takes no args and simply calls
> super().
>
> 1.2) If you do define one or more explicit
> constructors, regardless of whether they take args,
> then the compiler no longer provides the implicit
> no-arg ctor. In this case, you must explicitly define
> a public MyClass() {...}
if you want
> one.
>
> 1.3) Constructors are not inherited.
>
> 2) The first statement in the body of any ctor is
> either a call to a superclass ctor super(...)
>
or a call to another ctor of this class
> this(...)
>
> 2.1) If you do not explicitly put a call to
> super(...) or this(...) as the first statement in a
> ctor that you define, then the compiler implicitly
> inserts a call to super's no-arg ctor super()
>
as the first call. The implicitly called ctor
> is always super's no-arg ctor, regardless of whether
> the currently running ctor takes args.
>
> 2.2) There is always exactly one call to either
> super(...) or this(...) in each constructor, and it
> is always the first call. You can't put in more than
> one, and if you put one in, the compiler's implicitly
> provided one is removed.
I stil don't get it. Can you make it easier to understand, sorry for my stupidity but if apply to my codes, how it would look like?
You're calling super().
Do you know what that means? That means you're calling the superclass' constructor that takes no arguments.
You do see that your superclass' only constructor requires you to pass arguments, right?
If you don't create any constructors explicitly, there will be a no-arg constructor. But once you create an explicit constructor, that "automatic" no-arg constructor is gone. If you wnat one, you have to create it explicitly.
So either create a no-arg constructor in your superclass, or pass appropriate arguments to your existing super() call. I'd go for the latter.
You have other problems in your code as well, but I don't have time to go into them now.
After a while I have done this
public class Rectangle
{
private double length;
private double width;
private double ar;
public Rectangle(double length, double width)
{
this.length=length;
this.width=width;
}
public double area()
{
ar = length*width;
return ar;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
}
And
public class Box extends Rectangle
{
private double depth;
public Box(double length, double width, double depth)
{
super(length, width);
this.depth=depth;
}
public double newArea(double length, double width, double depth)
{
double newAr=2*(depth*length + depth*width + length*width);
return newAr;
}
public double Volume()
{
double vol=super.area()*depth;
return vol;
}
public static void main (String [] args)
{
Rectangle myRectangle=new Rectangle(3,4);
double display1=myRectangle.area();
myRectangle.getLength();
myRectangle.getWidth();
System.out.println(display1);
Box myBox= new Box(3,4,5);
double display2=myBox.newArea(3,4,5);
System.out.println(display2);
double display3=myBox.Volume();
System.out.println(display3);
}
}
The output is shown what I wanted, but I don't know whether I used correctly Inheritance or not, or there are some ways to cut down my codes?
And I thinkmyRectangle.getLength() and myRectangle() seems meaningless
Message was edited by:
Emotions
