Results 1 to 20 of 46
- 05-24-2010, 12:34 PM #1
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
- 05-24-2010, 01:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-24-2010, 03:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-24-2010, 03:30 PM #4
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
Thanks. I edited the thread in a much better way...
- 05-24-2010, 03:38 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
1. Look at the first constructor? Follow its logic, does it go anywhere?
5. Reread the information on static variables(Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)) but basically static variables go across instances, meaning calls to it from any instance will update it for all instances. The final modifier is used for constants.
7. You just did :p, but basically because since the Second class extends the First class, it can be treated as a instance of the class First. However, the methods that are specific to the Second class cannot be called on an instance of the First class, unless you typecast it first.
- 05-24-2010, 04:05 PM #6
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
1. I don't understand it's logic. Where does it go?1. Look at the first constructor? Follow its logic, does it go anywhere?
5. Reread the information on static variables(Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)) but basically static variables go across instances, meaning calls to it from any instance will update it for all instances. The final modifier is used for constants.
7. You just did , but basically because since the Second class extends the First class, it can be treated as a instance of the class First. However, the methods that are specific to the Second class cannot be called on an instance of the First class, unless you typecast it first.
5. Ok, I think I understand. getCount is a static method. It is better to be referenced with the class name, but it may be referenced with an object.
7. Well...since the Second extends First, Second inherits all of First methods, plus it has it's own. So, Second has all of First but First doesn't have all of Second. So is this what it means:
s1 is of type Second but it only gets the methods of First??
- 05-24-2010, 05:05 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
that call (this(num, num)), is calling the other constructor.Java Code:(snip) this (num, num); [COLOR="Red"] // what does that do/mean?[/COLOR] (snip) public First(int num1, int num2){ _num1 = num1; _num2 = num2; _count++; System.out.println ("First constructor2"); }
- 05-24-2010, 05:18 PM #8
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
That explains so much! thank you.that call (this(num, num)), is calling the other constructor.
Also, what doesdo?super.sum()
- 05-24-2010, 05:19 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
- 05-24-2010, 05:21 PM #10
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
super.method() calls the method inside the super class, so in this case super.sum() would call the sum method inside the First class.
- 05-24-2010, 05:26 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 05-24-2010, 05:31 PM #12
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
ok thanks! here's the rest...
9. f3 = s1; // but they are of different types! Don't you need to do casting here - f3 = (First)s1
// i'll wait to get the answer before I continue below...
10. System.out.println ("sum = " + f3.sum());
11. f3.setNum3 (2);
12. f4.setNum3 (2);
13. s1.setNum3 (2);
14. System.out.println ("sum = " + s1.sum());
15. s1 = f3;
16. s1 = f4;
17. System.out.println ("sum = " + s1.sum());
- 05-24-2010, 05:34 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
"public class Second extends First".
That is, an object of class Second is also an object of class First.
- 05-24-2010, 05:46 PM #14
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
- 05-24-2010, 05:52 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Not so sure what you mean by "aliasing".
The variables are simply references to an object in memory, so f3 = s1 is setting the reference in f3 to be the same as that of s1.
You're correct on 10, vecause f3 is now referencing an object of type Second.
You can do s1 = f3, but only by casting, to tell the compiler you actually mean it.
- 05-24-2010, 06:23 PM #16
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
ok, thanks.
11. f3.setNum3 (2); // according to my main method, this is an error! if f3 is now in Second, why doesn't this work??
12. f4.setNum3 (2); // this should be an error since f4 is in First
13. s1.setNum3 (2); // this should be fine
14. System.out.println ("sum = " + s1.sum()); // this should print sum of Second
15. s1 = f3; // now, s1 is still referring to Second since previously we wrote that f3 = s1
16. s1 = f4; // now s1 refers to First
17. System.out.println ("sum = " + s1.sum()); //why is this an error? I would think it would print the sum of First.
- 05-24-2010, 07:13 PM #17
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Can you please post all the steps again as this is getting harder and harder to follow
- 05-24-2010, 07:46 PM #18
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
public class First{
private int _num1 = 0;
private int _num2 = 0;
private static int _count = 0;
public First(int num){
this (num, num);
_count++;
System.out.println ("First constructor1");
}
public First(int num1, int num2){
_num1 = num1;
_num2 = num2;
_count++;
System.out.println ("First constructor2");
}
public int sum (){
return _num1 + _num2;
}
public static int getCount(){
return _count;
}
}
and the second class:
public class Second extends First{
private int _num3 = 0;
public Second(int num){
super (num);
_num3 = num;
System.out.println ("Second constructor1");
}
public Second (int num1, int num2, int num3){
super (num1, num2);
_num3 = num3;
System.out.println ("Second constructor2");
}
public int sum (){
return super.sum() + _num3;
}
public void setNum3 (int num){
_num3 = num;
}
}
1. First f1 = new First (10);
2. System.out.println ("sum1 = " + f1.sum());
3. System.out.println ("count = " + First.getCount());
4. First f3 = new First (10, 20);
5. System.out.println ("count = " + f3.getCount());
6. Second s1 = new Second (1);
7. First f4 = new Second (2);
8. System.out.println ("count = " + f4.getCount());
9. f3 = s1;
10. System.out.println ("sum = " + f3.sum());
11. f3.setNum3 (2); // according to my main method, this is an error! if f3 is now in Second, why doesn't this work??
12. f4.setNum3 (2); // this should be an error since f4 is in First
13. s1.setNum3 (2); // this should be fine
14. System.out.println ("sum = " + s1.sum()); // this should print sum of Second
15. s1 = f3; // now, s1 is still referring to Second since previously we wrote that f3 = s1
16. s1 = f4; // now s1 refers to First
17. System.out.println ("sum = " + s1.sum()); //why is this an error? I would think it would print the sum of First.Last edited by myst; 05-24-2010 at 07:49 PM.
- 05-24-2010, 08:01 PM #19
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
You declared f3 as a First object so it will still handle it as if it was a First object. There is no setNum3() method in the First class so this will error, unless you typecast f3 into a Second object like so
Another thing I'm noticing you are doing... You cannot do thisJava Code:((Second)f3).setNum3(2);
An object of class Second cannot be assigned an instance of class First.Java Code:First one = new First(); Second two = new Second(); one = two;
- 05-24-2010, 09:00 PM #20
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
But line 9 isYou declared f3 as a First object so it will still handle it as if it was a First object. There is no setNum3() method in the First class so this will error
so shouldn't that treat f3 as a Second, since an instance of First can be assigned an instance of Second?Java Code:f3 = s1;
I'm not doing it. It's a question which purposely has these mistakes. I'm trying to understand them.Another thing I'm noticing you are doing...
Similar Threads
-
Dealing with exceptions in my simple GUI app involving a process
By fawkes711 in forum AWT / SwingReplies: 2Last Post: 12-08-2009, 08:33 PM -
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
question about polymorphism
By becky in forum New To JavaReplies: 4Last Post: 02-11-2009, 10:59 PM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 01:38 AM -
Simple program involving military time
By busdude in forum New To JavaReplies: 4Last Post: 10-08-2008, 06:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks