Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

problem with inheritance

inheritance

  • Please log in to reply
6 replies to this topic

#1 csepraveenkumar

csepraveenkumar

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 54 posts

Posted 18 May 2011 - 05:55 AM

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.

  • 0

#2 csepraveenkumar

csepraveenkumar

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 54 posts

Posted 18 May 2011 - 06:00 AM

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?
  • 0

#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 18 May 2011 - 12:44 PM

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.
  • 0

#4 csepraveenkumar

csepraveenkumar

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 54 posts

Posted 19 May 2011 - 12:58 AM

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?
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 May 2011 - 01:16 AM

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.
  • 0

#6 csepraveenkumar

csepraveenkumar

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 54 posts

Posted 19 May 2011 - 01:48 AM

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.
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 May 2011 - 01:59 AM

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:
boxes.PNG
  • 0





Also tagged with one or more of these keywords: inheritance