Yet Another SCJP2
Originally posted by bill williams:
1. in case you might not know this:
class A{
int get(int i){return 1;}
}
class B extends A{
long get(int i){return 1;}//line 1
}
compiler error on line 1. i got 3 questions on this concept
2. remember that anonymous class can have argument like this:
addListener(new xxxListener(5){....};
3. remember how and why constructor is not inhereted
hope it helps
bill
8.4.8.3 Example: Incorrect Overriding
This example is an extended variation of that in the preceding section:
class Point {
int x = 0, y = 0, color;
void move(int dx, int dy) { x += dx; y += dy; }
int getX() { return x; }
int getY() { return y; }
}
class RealPoint extends Point {
float x = 0.0f, y = 0.0f;
void move(int dx, int dy) { move((float)dx, (float)dy); }
void move(float dx, float dy) { x += dx; y += dy; }
float getX() { return x; }
float getY() { return y; }
}
Here the class Point provides methods getX and getY that return the values of its fields x and y; the class RealPoint then overrides these methods by declaring methods with the same signature. The result is two errors at compile time, one for each method, because the return types do not match; the methods in class Point return values of type int, but the wanna-be overriding methods in class RealPoint return values of type float.
Originally posted by Manfred Leonhardt:
Hi Mark,
Straight from the JLS section 8.4.8.3:
This is a very important point and passing the test requires understanding of this crucial point!
Regards,
Manfred.
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Originally posted by bill williams:
1. in case you might not know this:
class A{
int get(int i){return 1;}
}
class B extends A{
long get(int i){return 1;}//line 1
}
compiler error on line 1. i got 3 questions on this concept
2. remember that anonymous class can have argument like this:
addListener(new xxxListener(5){....};
3. remember how and why constructor is not inhereted
hope it helps
bill
Rob
SCJP 1.4
Originally posted by Rob Ross:
Merely changing the return type of a method is not enough to overload it. To overload a method, it must have a different signature. The signature is the list of types in the argument list.
These would be overloaded foo methods:
void foo(int i);
void foo(int i, double d);
int foo(String s);
But these are not:
int foo(int i);
void foo(int i);
Rob
[ January 28, 2002: Message edited by: Rob Ross ]
remember how and why constructor is not inhereted
Originally posted by Praveena khandavalli:
hi rob,
i tried to compile the code and the error was at line 6: where the long get(int a) is declared saying that the method cant override the method in class A and for that it needs the same return type. so when a method is redeclared in a subclass as in this case... compiler looks for the same method name in the superclass whether it exists or not and if exists first checks the argument list and the return type and so the overriding or overloading of themethod is done.Am i right ...correct me if i am wrong please
-Praveena
The body of the constructor consists of an explicit constructor invocation (�8.8.5.1) of the form super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared.
Originally posted by Paul Salerno:
Regarding Annonymous Classes:
I was wondering if someone could elaborate this point with a code example perhaps, of why a constructor would not work within an annonymous class?
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Originally posted by Paul Salerno:
Valentin,
Yes I see that, but how does this relate specifically to annonymous inner classes?
If it is exactly the same signature (Has the same return type) then you CAN use it in the subclass.
If it is the same signature EXCEPT that it returns a different type, then it is illegal because it can't be overriding the method in the superclass and it can't be overloading since it has the same arguments.