• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

who can answer this question ?

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will be the output from this code and why ? What is the shortest way to make it display the correct result of 125 ?
<pre><>
class Rectangle{
int width, length , area;
Rectangle(int w , int l)
{
setArea(w,l);
}
void setArea(int w , int l)
{
width = w;
length = l;
area = w * l;
}
int getDimension(){return area;}
}
class Box extends Rectangle {
int height , volume;
Box(int w , int l , int h)
{
super(w,l);
height = w;
volume = height * width * length;
}
void setArea(int w ,int l)
{
volume = area * height;
}
int getDimension() {return volume; }
}
public class Test5
{
public static void main(String[] args)
{
Box b = new Box(5,5,5);
System.out.println(b.getDimension());
}
}
</pre>
Cheers
Sahir
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is because when you call setArea in the Rectangle constructor, you are going to get the method from the Box class because you overrode this method in Box, and which method it will run is determined at runtime.
To fix it, put a call to super.setArea(w,l); in the Box's setArea method.
Bill
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello bill,
Can we some how restrict to invoking only setArea method of the Rectangle class. One way is that we don't override this method in child class.
But i am talking about a case where there is an over ridden method but we only have to restric calling to the parent class setArea method.

Do i make any sense to you?
Regards,
Raj.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i can do it without calling any method(over head) as bill did. this can be done by just copying/shifting two lines of code from to setArea method to Rectangle constructor.
have a look....
<pre>
class Rectangle{
int width, length , area;
Rectangle(int w , int l)
{
width = w;
length = l;
setArea(w,l);
}
void setArea(int w , int l)
{
width = w;
length = l;
area = w * l;
}
int getDimension(){return area;}
}
class Box extends Rectangle {
int height , volume;
Box(int w , int l , int h)
{
super(w,l);
height = w;
volume = height * width * length;
}
void setArea(int w ,int l)
{
volume = area * height;
}
int getDimension() {return volume; }
}
class t
{
public static void main(String[] args)
{
Box b = new Box(5,5,5);
System.out.println(b.getDimension());
}
}
</pre>

some heavy modifications are also necessary in the original code.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest Solution to the original code might be making Rectangle's setArea method 'private'.
[This message has been edited by Nasir Khan (edited December 23, 2000).]
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more change should be with Box'constructor....
replace height = w; with height=h;
because if you create the Box object with different parameters e.g. Box b = new Box(5,1,2);You wouldn't get the desired
value of volume....
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Wow Again for me Java is a funny Language.
This time it is really making me to scratch my head. I was fully convinced from bill
"The reason is because when you call setArea in the Rectangle constructor, you are going to get
the method from the Box class because you overrode this method in Box, and which method it
will run is determined at runtime."
Because it is a case of Early Binding and Late Binding. At compile time it will go for the
class and at runtime it will go for actual object. As we all know that setArea() method is actually in Java is
this.setArea(). And at runtime this is object reference of Box method. So it will invoke setArea() method
of Box class.
Now when Naveen included these two extra lines in this code width = w;
length = l; it went for the setArea() method of Rectangle class. Why???
And when Nasir made setArea() method of Rectangle class then also it went for setArea()
method of Rectangle class. Why???
What am'I missing here???
Help me out.
Regards,
Raj.
 
naveen sahu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajpal Kandhari:
Hello,
Wow Again for me Java is a funny Language.



in that perticular class a lot of amount of code is redundant/unnecessary/worthless.
1) setArea method what is available in given class is absolutely westage. because if you are not having no args constructor in both of the class , then what is the need for setArea method.in the original code "writer/programmer/developer" wants to set the width & length through that method.that doesn't worth. instead of this if width & length variable declared as public & set explicitly , i think that will be much better. as available in java.awt.Point class.as available in java.awt.Dimension class etc.
2) the variable area & volume is meaningless. what are they doing there i can't understand.
3) the constructor of every class should be used as a initializer for the variables, not for doing any calculation or method call( if possible) because it's a bad habbit.
(like rectangle class constructor calling setArea in original code & box class constructor doing volume calculation).
and in last if java is funny so what are you doing here. without having the knowledge of java's "J" and "O" of oop's how can you think that you can analyse a given code of java. without knowing about flow control of code execution how can you decide that which method calling or variable shifting may cause effect on which portion of code.think about it !
and in the very last do you really know what was the writer of originally given code wants to do ? and which difficulty he feel ? and what he is asking in his post?
according to me what i know about java & oops the original code should be re-written as follows.
<pre>
class Rectangle{
int width, length ;
Rectangle(int w , int l)
{
width = w;
length = l;
}
int getDimension(){return width*length;}
}
class Box extends Rectangle {
int height ;
Box(int w , int l , int h)
{
super(w,l);
height = h;
}
int getDimension() {return height*length*width; }
}
class test5
{
public static void main(String[] args)
{
Box b = new Box(5,5,5);
System.out.println(b.getDimension());
}
}
</pre>
reply
    Bookmark Topic Watch Topic
  • New Topic