File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sonir, read pls

 
bill williams
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill...
Seems there are many traps in the exam right?..
Please let me know any other such traps..
thanx
Sonir
 
basha khan
Ranch Hand
Posts: 516
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u bill tip on ananymous class was very good
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>remember how and why constructor is not inhereted
this is in what context ? can you please elaborate.
and the method overiding for get(int i): well Bill i feel that this should be ok, because these are different methods now. as there return types are different. right ? overidding is between same methods in a class heirarchy. and these two are different methods.
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
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Straight from the JLS section 8.4.8.3:

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.

This is a very important point and passing the test requires understanding of this crucial point!
Regards,
Manfred.
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and there are 2 methods move in the RealPoint class, one is overiding correctly and other is overloading. right ?
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 Essouabni
Ranch Hand
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the return type is not enough for an overriding. It creates a compil error.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Yes you are correct. And why does the first one override correctly? All together now ...
Because the return types, parameter types, and parameter orders are identical
that's right class. A gold star for all!
Regards,
Manfred.
 
Praveena khandavalli
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bill,
can yu explain me why do yu get a compiler error at line 1 as yu ahve mentioned. i am confused..please explain.. correct me if i am wrong.. here the method seems to be overloaded... so why do yu get the error..
after a long time i am back to my preparation ...now i have to do it wihtout any breaks .please help me in that
thanx in advance

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 Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Praveena khandavalli
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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 ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding Annonymous Classes:

remember how and why constructor is not inhereted

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?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
have you read 15.9.5 Anonymous Class Declarations and following?
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only overidding is checked like you mentioned. overloading is different and return type is not in consideration at all in this case.
basically overidding is saying that both methods got to be the same in all respects, name, parameter list and return type.
overloading, only name is same, parameter list is different or order is different. return type is not in consideration at all. so merely changing return type will NOT overload a method.

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
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valetin,
Yes I just read this again

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.

But the code examples I have don't demonstrate an explicit constructor invocation of the super class. That's why I was hoping for a code example so that I could see it more clearly.
 
Praveena khandavalli
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot i got it
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go:

As you can see, line 1 won't compile because we implicitely make the compiler invoke the default no-arg ctor of class A which doesn't exist since another one is defined.
Line 2 works fine because the compiler will invoke the ctor taking one int argument.
Does it make it more clear ?
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,
Yes I see that, but how does this relate specifically to annonymous inner classes?
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


If you wanted to write a constructor in the anonymous inner class, what would you call it? A constructor is constructed by using the name of the class. But the anonymous inner class is anonymous. It has no name.

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
Valentin,
Yes I see that, but how does this relate specifically to annonymous inner classes?

The example really has to do with anonymous class.
Let me rewrite that properly:

The thing here is that you don't want to create a specific top-level class to subclass the abstract class A, so you just so it anonymously and provide a concrete implementation of all abstract methods of the class A (i.e. someMethod).
 
Erik Dark
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider:

I understand this is rejected because it's called a wrong way of overriding. But does it mean you can't ever use a method in a subclass with the same signature?
 
Tony Jackson
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Tony
 
Erik Dark
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

The first if I understand, it's overriding.
The second part in the second if...I know overloading is done within 1 class.
Am I right then to say you can never use a method in a subclass with the same signature but different return-type? Even when it's intended to be a whole new, different method? (Yes, I know the compiler doesn't know my intentions!)
Erik Dark
[ January 29, 2002: Message edited by: Erik Dark ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic