Results 1 to 17 of 17
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Call object methods from another class

    Please help, I've spent the past three hours tearing my hair out.

    Java Code:
    public class ProductDetailsClass  {
    private Products cheese = new Products("Cheese", 5.33, "cheese001", 
    "Rare Russian Goat Cheese 900g", false, false);
    }
    Java Code:
    public class Products {
    	
    	
    	String a;
    	double b;
    	String c;
    	String d;
    	boolean e;
    	boolean f;
    	
    			
    			public Products(String a, double b, String c, String d, boolean e, boolean f){
    					this.a = a;
    					this.b = b;
    					this.c = c;
    					this.d = d;
    					this.e = e;
    					this.f = f;
    				} //constructor closes
    
    //A few GETTER methods here
    
    }

    now I am trying to write code in another class, let's say InterfaceProcessor, and call the getter methods on the cheese object I created

    Java Code:
    Public class InterfaceProcessor{
    //call cheese getter methods
    }
    .


    I am failing miserably, getting all sorts of errors Can someone please help
    Last edited by africanhacker; 03-21-2011 at 11:36 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    It depends upon where and when you create an instance of the InterfaceProcessor class. If it is in the ProductDetails class then you pass the reference to your Product object to the constructor of the InterfaceProcessor class or perhaps using a method.
    Java Code:
    public class ProductDetails  {
        private Products cheese = new Products(blah, blah, blah);
        private InterfaceProcessor processor = new InterfaceProcessor(cheese);
    
        //....
            processor.setProduct(cheese);
    }

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can try giving it a reference to the object with composition. Also, use useful variable names instead of just a-f, using a-f makes for confusing code to read later.

    here is an example of composition

    Java Code:
    public class AClass{}
    
    public class Class1{
      private AClass aclass
      //constructor
      public Class1(AClass aclass){
        this.aclass = aclass;
      }
      public void doSomething(){
        //assume AClass has some getters and instance variables
        aclass.getX();
      }
    }

  4. #4
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I might be mistaken but I am not sure you got what I am asking for. I am not trying to create an instance of the InterfaceProcessor.

    What I want to do in the InterfaceProcessor class is work with the cheese reference I created in the ProductDetails class and use getter methods to collect and manipulate the cheese instance variables.

    All this has to happen in the InterfaceProcessor class.

    Now I know this is likely wrong but just to give you an idea of what I am trying to do.

    Java Code:
    ProductDetails.cheese.getPrice();
    This would then return a double for my use in the InterfaceProcessor class. The Products class has the relevant methods written into it. I know I sound a bit jumbled up but this is what I am trying to do. Please help :(
    Last edited by africanhacker; 03-21-2011 at 11:35 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't understand why you cant use composition?

    In main do the following
    Java Code:
    create cheese
    pass chese to interfaceProcessor constructor
    call InterfaceProcessor methods which call cheese methods
    You can't refer to the cheese variable outside of main because it doesn't exist outside of main.

  6. #6
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @Sunde887 you seem to be getting close to what I am asking for. Would you be kind enough to add some dummy code to your example.

    I understand the part about creating cheese. How about passing it to the InterfaceProcessors constructor, how would I do that. And after that how would I call the methods.

    Many thanks in advance

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Create interface processor like this
    Java Code:
    class InterfaceProcessrr{
      private ProductDetails aProduct;
     
      public InterfaceProcessor(ProductDetails aProduct){
        this.aProduct = aProduct;
      }
    }
    This makes it so the constructor initializes an instance of some product. So, if you wanted this class to print all the private information of some cheese product you can call the product instance variables getters.

    Finally in main do this

    Java Code:
    public static void main(String[] args){
      ProductDetails cheese = new(blah, blah, blah);
      InterfaceProcessor ip = new InterfaceProcessor(cheese);
      ip.printProduct();
    }

  8. #8
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks, let me give it a go. I will update you if I have good success, or not

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by africanhacker View Post
    I might be mistaken but I am not sure you got what I am asking for. I am not trying to create an instance of the InterfaceProcessor.
    HUH?

    If you don't create an instance of the InterfaceProcessor class then how is it supposed to do anything? Automagically? "Hey, I don't want a car but I want to be able to drive it to the shops"

  10. #10
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @Junky easy there. I'm just trying to understand.

    @Sunde887 I have failed to execute this. Here is the code that I have. I am trying to use the rice object methods in Interface Logic

    http://sosimpledesigns.com/problem.txt

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can you post the code up with code tags please?

  12. #12
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I could do that but the problem is that this is a school assignment and the moment my code is found online I am in serious trouble. That's why I posted a text link. I hope you understand and aren't put off by this inconvenience.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have created 20+ Product objects in the ProductDetails class, they are all private and there is no accessor methods. This means that it is impossible for any other class to access them. You need to rethink your design.

  14. #14
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @Junky, is there no way I can simply call the methods built into those objects? Do you have any suggestions as to how I could hack this to make it work or is this a lost cause :(

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Of course it is not a lost cause. Simply the way you have it now it is unworkable. You need to make changes. What those changes are will depend upon the requirements of your assignment. Chances are that you have misread or misunderstood some part of the assignment. Before you ask how to change it, no-one can aswer that without knowing what the assignment is.

  16. #16
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Okay I understand what you are saying. I seemed to be making decent progress until I hit this snag.

    I suppose my question would now be, is there anyway in Java that one can access the methods of an object from a different class. If I have to change the visibility modifiers from private to something else that is fine. My big problem is being able to call methods off those objects I have created.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by africanhacker View Post
    I suppose my question would now be, is there anyway in Java that one can access the methods of an object from a different class.
    That has always been your question and we have answered it multiple times. You need a reference to a ClassB object in ClassA (yet another code example below). That is the high level explanation. How to apply that to your particular case once again relies upon what the assingment says.
    Java Code:
    class Test {
        public static void main(String[] args) {
            ClassB bb = new ClassB();
            ClassA aa = new ClassA(bb);
            aa.doStuff();
        }
    }
    
    class ClassA {
        ClassB b;
    
        ClassA(ClassB bb) {
            b = bb;
        }
    
        public void doStuff() {
            b.print();
        }
    }
    
    class ClassB {
        public void print() {
            System.out.println("Hello World");
        }
    }

Similar Threads

  1. How to call an object from another class?
    By narty in forum New To Java
    Replies: 1
    Last Post: 11-01-2010, 09:14 PM
  2. Methods in Object class
    By Navatha in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 12:17 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  4. How to create object dinamically and call methods.
    By Gelembjuk in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 10:28 AM
  5. Replies: 0
    Last Post: 01-02-2008, 02:52 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
  •