hello everyone, I am new to java programming and i am currently studying inheritance.
I have following program and i am getting error "cannot find symbol" .Please help me.Thanks in advance.

class Box	{
	double width;
	double height;
	double depth;
	
	Box()	{		//default constructor
		width = 0;
		height = 0;
		depth = 0;
	}
	
	Box(double w, double h,double d)	{		//parametrised constructor
		width = w;
		height = h;
		depth = d;
	}
	
	Box(double x)	{		// for cube
		width = height = depth = x;
	}
	
	Box(Box ob)	{		//constructor which clones
		width = ob.width;
		height = ob.height;
		depth = ob.depth;
	}
	
	double volume()	{
		return width * height * depth;
	}
}

class BoxWeight	extends Box	{
	private double weight;
	
	BoxWeight(double w, double h, double d, double m)	{
		super(w, h, d);
		weight = m;
	}
	
	double GetBoxWeight()	{
		return weight;
	}
}

class boxinheritance	{
	public static void main(String args[])	{
		Box mybox = new Box();
		BoxWeight b2 = new BoxWeight(10,15,20,50);
		
		System.out.println("Box b1 volume = " + mybox.volume);
		System.out.println("Box b2 volume = " + b2.volume);
		System.out.println("Box b1 weight = " + b2.GetBoxWeight);
	}
}

Recommended Answers

All 11 Replies

i am getting error "cannot find symbol"

what symbol?

Post the entire error message that you are getting..

Can you post the entire error message that you are getting..

sagar@sagar-desktop:~/Dropbox/Java$ javac boxinheritance.java
boxinheritance.java:51: error: cannot find symbol
System.out.println("Box b1 volume = " + mybox.volume);
^
symbol: variable volume
location: variable mybox of type Box
boxinheritance.java:52: error: cannot find symbol
System.out.println("Box b2 volume = " + b2.volume);
^
symbol: variable volume
location: variable b2 of type BoxWeight
boxinheritance.java:53: error: cannot find symbol
System.out.println("Box b1 weight = " + b2.GetBoxWeight);
^
symbol: variable GetBoxWeight
location: variable b2 of type BoxWeight
3 errors

mybox.volume attempts to find a variable called volume in the Box class. You have no such variable (but you do have a method!).

what symbol?

Post the entire error message that you are getting..

hey actually the symbol is "."(dot) . in reply it is showing S from system.

mybox.volume attempts to find a variable called volume in the Box class. You have no such variable (but you do have a method!).

hey thanks .Thats my silly mistake.

double volume() {
return width * height * depth;
}

-------
double GetBoxWeight() {
return weight;
}

Those are methods ...
so you should call the methods by using () brackets.

The compiler is looking for variable named volume but it is method in your code.

System.out.println("Box b1 volume = " + mybox.volume);

So tell the compiler that it is method by using () brackets.

you should call the method as

System.out.println("Box b1 volume = " + mybox.volume());

mybox.volume attempts to find a variable called volume in the Box class. You have no such variable (but you do have a method!).

Oops ...@James cherill is fast in replying... :)

I din't refreshed the page....

Those are methods ...
so you should call the methods by using () brackets.

The compiler is looking for variable named volume but it is method in your code.

System.out.println("Box b1 volume = " + mybox.volume);

So tell the compiler that it is method by using () brackets.

you should call the method as

System.out.println("Box b1 volume = " + mybox.volume());

anyway thank u.It is running perfectly.

;-)
The secret of a fast reply is to keep it short!
This also ensures one doesn't give too much of the complete answer - just enough to get the OP thinking along the right lines.
@sagy - if your problem is answered correctly, please mark this thread "solved".

;-)
The secret of a fast reply is to keep it short!
This also ensures one doesn't give too much of the complete answer - just enough to get the OP thinking along the right lines.
@sagy - if your problem is answered correctly, please mark this thread "solved".

Yeah . i agree..KISS (keep it short and simple)

Just practicing my typing..:)

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.