*edit
edit edit edit
Printable View
*edit
edit edit edit
Thanks. I edited the thread in a much better way...
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.
1. I don't understand it's logic. Where does it go?Quote:
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??
that call (this(num, num)), is calling the other constructor.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");
}
That explains so much! thank you.Quote:
that call (this(num, num)), is calling the other constructor.
Also, what doesdo?Quote:
super.sum()
super.method() calls the method inside the super class, so in this case super.sum() would call the sum method inside the First class.
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());
"public class Second extends First".
That is, an object of class Second is also an object of class First.
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.
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.
Can you please post all the steps again as this is getting harder and harder to follow
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.
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 thisCode:((Second)f3).setNum3(2);
An object of class Second cannot be assigned an instance of class First.Code:First one = new First();
Second two = new Second();
one = two;
But line 9 isQuote:
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
so shouldn't that treat f3 as a Second, since an instance of First can be assigned an instance of Second?Code:f3 = s1;
I'm not doing it. It's a question which purposely has these mistakes. I'm trying to understand them.Quote:
Another thing I'm noticing you are doing...