File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can Any One Explain Me The Following Codes Briefly 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 "Can Any One Explain Me The Following Codes Briefly" Watch "Can Any One Explain Me The Following Codes Briefly" New topic
Author

Can Any One Explain Me The Following Codes Briefly

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61








Code1: No Compile Error
Code2: Compile Error
Code3: Compile Error



[ October 09, 2008: Message edited by: Thangaraj Raj ]
[ October 09, 2008: Message edited by: Thangaraj Raj ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Well Thangaraj this is a unique kind of a problem. This is why you had to wait for two days. And this is also not a perfect solution that I am providing. I compiled your codes and it gives this error message

name clash: <T>say() in Child and say() in Parent have the same erasure, yet neither overrides the other.

In both the second and the third code, the error is similar. Somehow the compiler is not able to deduce that the say method in Child class is overriding the say method in Parent class. So it says that they have the same name but say in child doesn't override say in Parent.

I also tried to add an @Override annotation, but that too gives an error that no method is being overriden. the 2nd example is especially shocking. You have just added a <T> and haven't even used it anywhere. But still the compiler gives a warning.

I read through Generics FAQ by Angelika Langer, and it says on page 466

a class (subclass in this case) must not have methods with identical erasures


The FAQ also states that a non-generic method can override a generic method but not vice versa, as is happening in your case.

So basically you have to memorize it for now. If I get a reason for this behavior, I will definitely help you.

If anyone has any idea, please comment...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
thank you very much...
Actually I am preparing For scjp just finished reading the kathy sierra bert bates...
now next what i want to do...
what type of mock exam i want to take.. can you give some valuable sites for mock test.
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
the thumb rule for overriding is that the overriden method cannot have more visibility than its parent method.

The generic overloaded meethod is trying to do the same thing.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
the thumb rule for overriding is that the overriden method cannot have more visibility than its parent method.

The generic overloaded meethod is trying to do the same thing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Originally posted by Harshit Rastogi:
the thumb rule for overriding is that the overriden method cannot have more visibility than its parent method.
I am afraid you have got that the wrong way round; the overridden method cannot have less "visibility" than its superclass counterpart.

If the superclass method is public, then it can only be overridden as public; if it is default or protected it cannot be overridden as private.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

@Harshit I agree with Campbell. Also I don't know how it is applicable here as all the methods have default(package) visibility.

@Thangaraj you can view a list of good free and paid mock exams in the javaranch SCJP mock exams list
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Any One Explain Me The Following Codes Briefly
 
Similar Threads
Thread Prog...
RelationShip Between Iterator And Vector...
Overriding generic methods
Overriding and Generics Isse
Question in Generics -2