Results 1 to 4 of 4
Thread: A very confusing question
- 05-08-2011, 06:53 AM #1
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
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?
- 05-08-2011, 07:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 05-08-2011, 07:12 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Each person will have there own bag. The bag should be initialized in the persons constructor.
Ill let you discover how all this plays out. Add some code and allow changes to the different players bags and observe what happens.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); } }
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.
- 05-08-2011, 07:29 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Use super().How do I call a method in a superclass if I have created an object from it's subclass?
[Edit] Really slow !;( while I considered replying to what I thought the OP really had in mind here...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; } }
Similar Threads
-
confusing ques
By rok0016 in forum New To JavaReplies: 6Last Post: 07-15-2010, 06:07 PM -
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10-16-2009, 11:06 PM -
Confusing input question
By jigglywiggly in forum New To JavaReplies: 1Last Post: 03-13-2009, 04:38 AM -
Confusing IndexOutOfBounds Error?
By tibbyuk in forum New To JavaReplies: 2Last Post: 08-08-2008, 11:42 PM -
Confusing with Frameworks
By hisouka in forum New To JavaReplies: 0Last Post: 08-05-2008, 05:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks