Q>Constructors! how to pass a value from MainClass to SubClass and back to MainClass?
i want to know about constructors..
i have a value from MainClass which is "a=1",
i want to pass it to SubClass and add "a=a+5" and print tha "a" value in SubClass,
and last the new value of "a" in SubClass which is "6" and pass again to the MainClass
and change the original value of MainClass "a=1" to new value of "a=6" from SubClass..
Code:
class MainClass {
public static void main(String[] args) {
SubClass SubClassObject = new SubClass();
int a = 1;
SubClassObject.putNumber(a);
System.out.println("the value of a in MainClass :" + a);
}
}
Code:
public class SubClass {
public void putNumber(int a) {
a = a + 5;
System.out.println("the value of a in SubClass :" + a);
}
}
original output:
the value of a in SubClass :6
the value of a in MainClass :1
i want this output:
the value of a in SubClass :6
the value of a in MainClass :6
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Change the putNumber(...) method to return the incremented value, and assign that return value to a in MainClass.
db
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Your example doesn't make a lot of sense. Your example mutes the power of inheritance. First your subclass must extend your Main class using the extends key word.
Code:
public class SubClass extends MainClass
{
}
your subclass will have access to any instance variables that are public or protected in your MainClass (super Class)
I think what you are trying to accomplish would make sense only like this:
Code:
public class MainClass
{
protected int a = 0;
//constructor
publicMainClass()
{
a = 1;
}
}
then:
Code:
public class SubClass extends MainClass
{
//Constructor
public SubClass()
{
super.a += 5;
}
}
Remeber Java allows you to treat an object of a sub class as an object of its super class (polymorphism).
That's why your example is a little on the odd side.
hope this helps!
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
Originally Posted by
DarrylBurke
Change the putNumber(...) method to return the incremented value, and assign that return value to a in MainClass.
db
darryl teach me how to change the method to return the increment value and assign the return value?
whats set of codes im going to add in the subclass to solve me problem?
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
Remeber Java allows you to treat an object of a sub class as an object of its super class (polymorphism).
That's why your example is a little on the odd side.
hope this helps!
mwr1976.. ahh u can use extends also hmm tnx!
can u aply also without using "extends" like ordinary codes like in my SubClass adding only small codes to get the same output without using extends?
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
You use the term "sub class". This means that it is a derived class. In order for a class to be a "sub class" (derived class) you have to extend your "base class" It might be a little easier to help you if you could explain why you are trying to do this.
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
Originally Posted by
mwr1976
You use the term "sub class". This means that it is a derived class. In order for a class to be a "sub class" (derived class) you have to extend your "base class" It might be a little easier to help you if you could explain why you are trying to do this.
ahm just imagine subclass is not the subclas of mainclass, imagine its just another javafile want to send a value and to call it back with a new value..
ahm is there other way to return the value a of subclass "a" to back to mainclass "a"?
plz help me im still a nobie in java..plz!
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
way to return the value a
Use the return statement in the method with the value you want returned to the caller of the method:
return a;
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
"i have a value from MainClass which is "a=1",
i want to pass it to SubClass and add "a=a+5" and print tha "a" value in SubClass,
and last the new value of "a" in SubClass which is "6" and pass again to the MainClass
and change the original value of MainClass "a=1" to new value of "a=6" from SubClass.."
There is no way to do this. The variable "a" only has scope within the class that it is created in. If you want to work with that exact variable your only choice is to use inheritance(extending the base class). The only other thing you can do(which has nothing to do with constructors) is to create a method that adds 5 to whatever parameter is passed to it and have it return the result to the caller.Lets call the method "AddTo()". you would have to to create a subclass instance and then call the AddTo() method.
it would be something like:
Code:
public int AddTo(int num)
{
return num + 5;
}
Code:
SubClassInstance.AddTo(a);
hope it helps!
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
You would want to assign the value returned to a variable (for example: a) otherwise it will be lost.
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
Originally Posted by
Norm
You would want to assign the value returned to a variable (for example: a) otherwise it will be lost.
what codes il use? or can u edit my SubClass an put return, i dont know how to use return.
is it okay if u edit my Codes in SubClass.. thnks!
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
where exact line im going to insert this codes in my SubClass?
Code:
public int AddTo(int num)
{
return num + 5;
}
And this codes where should i put it in my MainClass or SubClass?
Code:
SubClassInstance.AddTo(a);
hope it helps![/QUOTE]
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
line 3 returns a value from the addTo() method.
To receive the returned value, assign what is returned by the method in a variable:
a = subClassInstance.addTo(a); // pass a to method and save the returned value back into a
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
norms i tried it in line 3 and eror ahm can u help me edit my entire main and subclas?
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
ahm can u edit my codes i cant get it right plz norm?
i cant get it right plz..
Code:
class MainClass {
public static void main(String[] args) {
SubClass SubClassObject = new SubClass();
int a = 1;
SubClassObject.putNumber(a);
System.out.println("the value of a in MainClass :" + a);
}
}
Code:
public class SubClass {
public void putNumber(int a) {
a = a + 5;
System.out.println("the value of a in SubClass :" + a);
}
}
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Quote:
i cant get it right
What is "right"?
Please explain what you want the code in post#15 to do.