By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Passing values between multiple classes

P: 5
Hi,
I just started learning JAVA and I'm really NOT sure how to manipulate values in different classes.
for example. I have a class 'class1' which updates some variables for its own use.
then I have another class 'class2' which should take the result of the computation in 'class1' and use that value to perform some other operations.

I then have the main program file in a separate file that returns the result of the two classes.

is this possible? I know that from my main program, I can change the values on the classes. but I cannot seem to get values from one class to another.

class1 code
public class Class1
{
double length; //line size

public Class1( double length )
{
setLength( length ); //calls method setLength
}
public void setLength( double lengthValue )
{
length = lengthValue;
}
public double getLength()
{
return length;
}
public String toString()
{
return "Line Size = " + length;
}
}


//THE SECOND CLASS
public class Class2 extends Class1{
public double width;

public Rectangle( double lengthValue)
{
width = lengthValue;
}
public void setWidth( double lengthValue )
{
width = lengthValue; //I THINK THIS IS WHERE THE PROBLEM IS
}
public double getWidth()
{
return width;
}
public double getSum()
{
return width * 2;
}
public String toString()
{
return "Sum is (" + width + " * " + 2 + ") = " + getSum();
}
}

when I run the main problem the toString message above displays 0

again,
thank you for all your help...

BB
Apr 29 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
1.) Please use code tags if you have to post code.
2.)Use appropriate names for your classes (your class2 above will not compile).
3.)You can use getters to get data from objects or pass an entire object to the constructor of another (deepending on your design).
Apr 30 '07 #2

Expert 10K+
P: 11,448
moderator note: I have removed the offending message and two replies so that
the show can go on as usual.

kind regards,

Jos
Jun 24 '08 #3

Expert 100+
P: 785
How to get values from your base class (Class1):
read about super() if you want to call the toString() method (that you overload)from your base class (Class1) in your extended class (Class2).
You can also pass values into your extended class if you call getter and setter methods from your base class which are not overloaded.

Why it returns 0 and not for example 4 in your getSum() method if you call it with "new Class2(2)" is unclear to me; it should do it right from what I have seen. Maybe you have an error in your main() program ? I mean maybe you call it the wrong way?
Please post your main program where you call these classes.
Jun 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.