Results 1 to 4 of 4
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default A very confusing question

    Hello
    :confused:
    I am wondering if I created an object from a class that created an object, would the object that was in the class that I created the second object from be unique?

    For example, I create a class called Inventory and then I create a class called Human. In the Human Class I create an Inventory object called bag and then in my main application I create a Human object called John and a human object called Dave.

    Would John.bag.method() affect the same bag as Dave.bag.method()???



    Another question, How do I call a method in a superclass if I have created an object from it's subclass?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Would John.bag.method() affect the same bag as Dave.bag.method()?

    In general they would not be the same.

    Typically methods and member variables are different. So john.bag would be different from dave.bag (just like john.name, which might be the String "John" would be different to dave.name). In the same way the Inventory instance john.bag would be a different thing from the Inventory instance dave.bag.

    (john and dave would be better variables for the Human instances. Uppercase is usually used for starting class names).

    There are so called static or class variables that are shared by all instances. (See, eg, mindprod's glossary entry on static and the warning therein about overuse.) The comments made above do not refer to these and assume neither bag nor method() are static.

    ------------

    But, of course, the best way of answering your question is to write some code:

    Java Code:
    void method() {
        System.out.println("method() being called on instance " + this);
        // etc
    }
    Last edited by pbrockway2; 05-08-2011 at 07:06 AM.

  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

    Each person will have there own bag. The bag should be initialized in the persons constructor.
    Java Code:
    class Inventory{}
    class Human{
      Inventory bag;
      public Human(Inventory bag){
        this.bag = bag;
      }
      public Human(){
        bag = new Inventory();
      }
      public static void main(String[] args){
        Human h = new Human();
        Human bob = new Human();
        Inventory b = new Inventory();
        Human chris = new Human(b);
        Human mike = new Human(b);
      }
    }
    Ill let you discover how all this plays out. Add some code and allow changes to the different players bags and observe what happens.


    If the method of the super class is public it is inherited by the subclass, just use the method. If you overrode the super class method you can call the super classes version with the super keyword.

    Java Code:
    super.methodName();
    Last edited by sunde887; 05-08-2011 at 07:18 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How do I call a method in a superclass if I have created an object from it's subclass?
    Use super().

    Java Code:
    public class Child {
    	private String name;
    	
    	public Child(String name) {
    		this.name = name;
    	}
    	
    	public String describe() {
    		return "I am " + name;
    	}
    }
    
    class Parent extends Child {
    	private Child child;
    	
    	public Parent(String name, Child child) {
    			// use super to invoke the superclass's constructor
    		super(name); 
    		this.child = child;
    	}
    
    	@Override
    	public String describe() {
    			// use super to invoke the superclass's method
    		return super.describe() + " the parent of " + child;
    	}
    }
    [Edit] Really slow !;( while I considered replying to what I thought the OP really had in mind here...

Similar Threads

  1. confusing ques
    By rok0016 in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 06:07 PM
  2. Confusing with bitwise NOT operator
    By Willi in forum New To Java
    Replies: 4
    Last Post: 10-16-2009, 11:06 PM
  3. Confusing input question
    By jigglywiggly in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 04:38 AM
  4. Confusing IndexOutOfBounds Error?
    By tibbyuk in forum New To Java
    Replies: 2
    Last Post: 08-08-2008, 11:42 PM
  5. Confusing with Frameworks
    By hisouka in forum New To Java
    Replies: 0
    Last Post: 08-05-2008, 05:52 AM

Posting Permissions

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