aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sonir, read pls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sonir, read pls" Watch "sonir, read pls" New topic
Author

sonir, read pls

bill williams
Ranch Hand

Joined: Jan 15, 2002
Posts: 94
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


Yet Another SCJP2
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Jan 26, 2002
Posts: 516
thank u bill tip on ananymous class was very good
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
>>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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 13, 2002
Posts: 479
Changing the return type is not enough for an overriding. It creates a compil error.


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Jan 05, 2002
Posts: 21
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

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
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

Joined: Jan 17, 2002
Posts: 172
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

Joined: Aug 26, 2001
Posts: 7610
Paul,
have you read 15.9.5 Anonymous Class Declarations and following?


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 17, 2002
Posts: 172
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

Joined: Jan 05, 2002
Posts: 21
thanx a lot i got it
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jan 17, 2002
Posts: 172
Valentin,
Yes I see that, but how does this relate specifically to annonymous inner classes?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.



JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jan 28, 2002
Posts: 107
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

Joined: Feb 23, 2001
Posts: 45
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

Joined: Jan 28, 2002
Posts: 107
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sonir, read pls