Results 1 to 16 of 16
  1. #1
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default 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..

    Java 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);
    	}
    }

    Java 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
    Last edited by boblingwide; 04-09-2012 at 11:24 AM. Reason: speling

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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.
    Java 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:
    Java Code:
    public class MainClass
    {
          protected int a = 0;
         
         //constructor
         publicMainClass()
         {
             a = 1;
         }
    }
    then:
    Java 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!

  4. #4
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    Quote Originally Posted by DarrylBurke View Post
    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?

  5. #5
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    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?

  6. #6
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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.

  7. #7
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    Quote Originally Posted by mwr1976 View Post
    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!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    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;
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    "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:
    Java Code:
    public int AddTo(int num)
    {
         return num + 5;
    }


    Java Code:
    SubClassInstance.AddTo(a);
    hope it helps!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    Quote Originally Posted by Norm View Post
    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!

  12. #12
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default 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?
    Java Code:
    public int AddTo(int num)
    {
         return num + 5;
    }

    And this codes where should i put it in my MainClass or SubClass?
    Java Code:
    SubClassInstance.AddTo(a);
    hope it helps![/QUOTE]

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default 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?
    Last edited by boblingwide; 04-10-2012 at 12:16 PM. Reason: speling

  15. #15
    boblingwide is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default 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..

    Java 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);
        }
    }
    Java Code:
    public class SubClass {
         
        public void putNumber(int a) {
            a = a + 5;
            System.out.println("the value of a in SubClass :" + a);
        }
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl

    i cant get it right
    What is "right"?
    Please explain what you want the code in post#15 to do.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. pass data from Jdialog back to jFrame
    By Rogue45 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2012, 01:35 AM
  2. Scriptlet Not Running on JSP after coming back from back button of browser
    By jason.3dmagic in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-23-2011, 08:44 AM
  3. Replies: 0
    Last Post: 12-02-2010, 05:54 AM
  4. Subclass help
    By amystauff in forum New To Java
    Replies: 1
    Last Post: 05-30-2010, 05:36 AM
  5. Replies: 1
    Last Post: 03-04-2009, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •