I know that if a instance variable of a class is made private
then it is not accessed directly by the object.
But in the following code,something as such is happening,and the code is running fine!
How is b
able to directly access length,breadth and height?
public class Box {
private int length,breadth,height;
Box(int a,int b,int c){
length=a;
breadth=b;
height=c;
}
Box(Box b){
length=b.length; //These lines!
breadth=b.breadth;
height=b.height;
}
int volume(){
return length*breadth*height;
}
}
public class BoxWeight extends Box{
public int weight;
BoxWeight(int a,int b,int c,int d){
super(a,b,c);
weight=d;
}
BoxWeight(BoxWeight b){
super(b);
weight=b.weight;
}
}
class apples
{
public static void main(String[] args)
{
BoxWeight mybox1=new BoxWeight(10,20,30,40);
BoxWeight clone=new BoxWeight(mybox1);
System.out.println(mybox1.volume()+"...."+mybox1.weight);
System.out.println(clone.volume()+"...."+clone.weight);
}
}