Jump to content

problem with inheritance

- - - - -

  • Please log in to reply
6 replies to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
class box{
double width;
double height;
double depth;
}
class bigbox extends box{
double weight;
}
class example49{
public static void main(String args[]){
bigbox b=new bigbox();
b.height=10;
b.width=20;
b.depth=30;
b.weight=50;
}
}
the above code compiles. if i add a constructor to the class box like below
class box{
double width;
double height;
double depth;
box(box a){
width=a.width;
height=a.height;
depth=a.depth;
}
class bigbox extends box{
double weight;
}
class example49{
public static void main(String args[]){
bigbox b=new bigbox();
b.height=10;
b.width=20;
b.depth=30;
b.weight=50;
}
}
then i get the following error on compiling
example49.java:11: cannot find symbol
symbol  : constructor box()
location: class box
class bigbox extends box{
^
1 error
if i again add a constructor to the class bigbox like below
class box{
double width;
double height;
double depth;
box(box a){
width=a.width;
height=a.height;
depth=a.depth;
}
}
class bigbox extends box{
double weight;
bigbox(){
height=-1;
width=-1;
depth=-1;
weight=-2;
}
}
class example49{
public static void main(String args[]){
bigbox b=new bigbox();
b.height=10;
b.width=20;
b.depth=30;
b.weight=50;
//box a=new box(b);
}
}

i get the following error on compiling
example49.java:13: cannot find symbol
symbol  : constructor box()
location: class box
bigbox(){
        ^
1 error
what is causing these errors?
help please?

Edited by csepraveenkumar, 18 May 2011 - 06:28 AM.


#2
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
found that the solution was adding a constructor box(){...} to the class box, but why do i have to add this constructor even though i am not declaring any object of class box? also this problem only comes up when the class bigbox has a constructor which takes no arguments. when bigbox has no constructors or constructors that take some arguments then the program compiles fine?

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
When you create a BigBox, java will automatically first create a Box. Java does this by calling the no-arg constructor by default.
(If you don't write any constructor, java creates an emtpy no-arg constructor for you - If you DO have a constructor, java won't make an empty one.)

To avoid java calling the no-arg constructor of Box, you need to specify wich constructor it must take as 1st line of the BigBox's constructor:

bigbox(Box a){

  [B]super(a)[/B] 

  height=-1;

  width=-1;

  depth=-1;

  weight=-2;

}

This will propably work, even when Box has no no-arg constructor. Because you tell Java to take the other one instead.

#4
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
does this mean that is i want to have a no-arg constructor in the class bigbox, then i should first have a no-arg constructor in class box first?

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It means that if class A extends class B, AND class A's constructor doesn't reference class B's constructor as first line(see bold part in previous post),
then B must have a no-arg constructor, as that's the default Java will take.

If B doesn't have any constructor Java will create a no-arg constructor by itself. But if B allready has one, Java won't make one for you.

How the constructor of A(bigbox) looks, how many parameters it has, doesn't matter.

#6
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
ok..... just got it..... if super() is used in a subclass constructor then it does not matter whether the superclass has a no-arg constructor. But if subclass does not use super() then the subclass will automatically call the no-arg constructor of the superclass and will give error if the no-arg constructor of superclass is not present.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, doing super() would call the no-arg constructor aswell.

Here's an image with, I think, all possible situations.
1 box class with no cosntructor at all, 1 with a constructory WITH args, and 1 with a constructor WITH args, but also without.
Then bigBox will have 3 different constructors, calling each different super constructor:
Attached File  boxes.PNG   27.25K   9 downloads




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users