permaculture playing cards*
The moose likes Beginning Java and the fly likes (Inheritance)  please Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "(Inheritance)  please Help!" Watch "(Inheritance)  please Help!" New topic
Author

(Inheritance) please Help!

Manikantan Nair
Greenhorn

Joined: Oct 13, 2000
Posts: 13
I'm a JAVA student. And no exp. in C++.
I can't understand what is gping wrong with the following programm. Please help me to correct it. Thanks in Advance.
//a simple inheritance
class box
{
double width;
double height;
double depth;
box(box obj){width=obj.width;height=obj.height;depth=obj.depth;}
box(double w, double d, double h)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return width*depth*height;
}
}
class suBox extends box
{
double weight;

suBox(double w, double d, double h, double m)
{
depth=d;
height=h;
width=w;
weight=m;
}

}
public class ex9inhert
{
public static void main(String args[])
{
suBox box1=new suBox(30, 20, 10, 45.34);
box box2=new box(30,20,40);

System.out.println(box2.volume());
System.out.println(box1.volume());
System.out.println(box1.weight);
}
}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The problem is that box doen't have a default constrictor and you aren't specifically calling any constrictor in box. You can correct by changing the constrictor in suBox to:
suBox(double w, double d, double h, double m)
{
super(w, d, h);
weight=m;
}
When you don't make a specific call to a super classes constrictor as the first call in the subclasses constrictor then java will try to construct an object of the super class using a default constrictor(one without any arguments). Java with provide an empty default constrictor automatically if and only if you don't provide a constrictor at all. Once you create a constrictor that takes arguements however, if you want a default constrictor you must write it yourself.
I hope this helps


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Jayashree Varahan
Greenhorn

Joined: Sep 17, 2000
Posts: 9
Please change the constructor in suBox. It should be
suBox(double w, double d, double h, double m)
{
super(d,h,m);
weight = w;
}
The base class constructor call must be the first statement in the body of the derived class constructor. If the first statement in a derived class constructor is not a call to a base class constructor, the compiler will insert a call to the default base class constructor for you: super()
Manikantan Nair
Greenhorn

Joined: Oct 13, 2000
Posts: 13
Thank you very much. it worked.
But, if you have the book "The Complete Reference"
by Patrick Naughton, please check the page 195.
In that Example there is no call to the "super class constructer".
Is it wrong or what?
Thank you once again for your help.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Sorry, I don't have that book. If you post the code I can explain it. My guess is that the super class has a default constructor. Your program would have worked just as well (but would have been a lot less clear) if you would add
box(){}
to your original box class.
Hope this helps
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
When you create any class, if you don't write a constructor for it, the compiler automatically creates a default constructor.
For example
class Person{
//Lots of methods/variables but no constructor
}
the compiler will create a default constructor Person(){} for you. If you write a constructor for Person, even if its signature is different, the default constructor is not created by the compiler.
Person(){
Person(String name)
{
// Now I've added this constructor, the default
// constructor Person(){} won't be created by the compiler
}
}
Similarly, when you write a constructor for a subclass, there is always a call to the super class's constructor. Either you can put in a call to the super class's constructor - this has to be the first thing in the subclass's constructor. If you don't do this, the compiler inserts a call to the super class's constructor:-
super();
The compiler will look for a constructor in the superclass that takes no arguements - if there isn't one (either the default constructor or one you've written), the compiler will complain!
class Employee extends Person{
Employee()
{
// even though I don't write it, the compiler inserts a call to
// the constructor of Person here super();
System.out.println("Employee class constructor");
}
}
This code will compile fine with the first version of Person class which has a default constructor but it won't work with the second version because that only has one constructor, Person(String name){}. I can solve this problem two ways - either by adding another constructor to Person:_
Person()
{
// another constructor taking no arguements
}
or by putting in an explicit call to super at the start of Employee's constructor:-
Employee()
{
super("John");
System.out.println("Employee constructor");
}
Hopes this makes things a bit clearer!
Kathy
Manikantan Nair
Greenhorn

Joined: Oct 13, 2000
Posts: 13
thank you all of you.
I got stuck because, I haven't added the default
constructer in supper class.
If there is no default constructer (means constructer without
arguements) in the super class we must call a constructer
from the subclass. Otherwise compiler will complain!!
thank you all of you again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: (Inheritance) please Help!