Hello,I have the following two interfaces.//-----------------------public class B extends A//-----------------------public interface IA { public A getFormat(); public interface IB extends IA { public B getFormat(); }}//-----------------------Here, interface IB overrides the getFormat() method of its super-interface IA. Instead of returning an A it now returns a B. Note that B extends A.In my eclipse, if I choose a compiler compliance level <= Java 1.4 this gives an error "The return type is incompatible with IA.getFormat()". If I choose a compliance level >= 5.0 it is OK without problem.Is the overriding mechanism really different between Java 1.4 and 5? (couldn't find any info about this on the new features page of J2SE 5.0)What can I do to still use this construct on a Java 1.3 compatible JVM?ThanksPhil
|
|
0
|
|
|
|
Reply
|
sicsicsic (164)
|
1/8/2008 3:53:11 PM |
|
Philipp wrote:> In my eclipse, if I choose a compiler compliance level <= Java 1.4 this > gives an error "The return type is incompatible with IA.getFormat()". If > I choose a compliance level >= 5.0 it is OK without problem.I think I remember hearing something about this. It's not inheritance, per se, but the allowed return types when overriding a method. 1.5 is allowed to be looser (type or subtype) than 1.4 (same type only). Sorry I don't have a JLS reference for you.1.3 is unaffected by this change....
|
|
0
|
|
|
|
Reply
|
Mark
|
1/8/2008 3:57:45 PM
|
|
On Tue, 08 Jan 2008 16:53:11 +0100, Philipp <sicsicsic@freesurf.ch>wrote, quoted or indirectly quoted someone who said :>What can I do to still use this construct on a Java 1.3 compatible JVM?You can't. There methods must differ in more than just return type.-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
1/8/2008 4:30:32 PM
|
|
Philipp <sicsicsic@freesurf.ch> wrote:> public interface IA {> public A getFormat();> public interface IB extends IA {> public B getFormat();> }> }> What can I do to still use this construct on a Java 1.3 compatible JVM?Change interface IB's method to also return A. Sorry, no other way. (any class that implements IB will of course still return B objects at runtime, but they can't state this fact such that old JVM knows.)
|
|
0
|
|
|
|
Reply
|
Andreas
|
1/8/2008 5:04:38 PM
|
|
Andreas Leitgeb wrote:> Philipp <sicsicsic@freesurf.ch> wrote:>> public interface IA {>> public A getFormat();>> public interface IB extends IA {>> public B getFormat();>> }>> }>> What can I do to still use this construct on a Java 1.3 compatible JVM?> > Change interface IB's method to also return A. > Sorry, no other way. (any class that implements IB will> of course still return B objects at runtime, but they> can't state this fact such that old JVM knows.)This is called "covariant return", and was introduced in 1.5. An overriding method may return a subtype ("covariant type") of the parent method's return type. You cannot have two different overrides that differ only in return type, though.In 1.4 and earlier (you do realize that 1.3 has been retired for some time now, don't you? And that 1.4 hits its "End-of-Life" this very year?) covariant return types are not permitted.<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8>Specifically ss. 8.4.8.3.> If a method declaration d1 with return type R1 overrides or hides the declaration > of another method d2 with return type R2, then d1 must be return-type substitutable > for d2, or a compile-time error occurs. Furthermore, if R1 is not a subtype of R2, > an unchecked warning must be issued (unless suppressed (§9.6.1.5)).-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
1/9/2008 2:54:25 AM
|
|
Lew wrote:> Andreas Leitgeb wrote:>> Philipp <sicsicsic@freesurf.ch> wrote:>>> public interface IA {>>> public A getFormat();>>> public interface IB extends IA {>>> public B getFormat();>>> }>>> }>>> What can I do to still use this construct on a Java 1.3 compatible JVM?>>>> Change interface IB's method to also return A. Sorry, no other way. >> (any class that implements IB will>> of course still return B objects at runtime, but they>> can't state this fact such that old JVM knows.)> > This is called "covariant return", and was introduced in 1.5. OK. I didn't know the correct term. This use of the word "covariant" somewhat differs from the mathematical sense...> <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8>> Specifically ss. 8.4.8.3.Thanks for the ref.Phil
|
|
0
|
|
|
|
Reply
|
Philipp
|
1/9/2008 8:56:17 AM
|
|
On Tue, 08 Jan 2008 16:53:11 +0100, Philipp <sicsicsic@freesurf.ch>wrote, quoted or indirectly quoted someone who said :>Is the overriding mechanism really different between Java 1.4 and 5? >(couldn't find any info about this on the new features page of J2SE 5.0)see http://mindprod.com/jgloss/covariance.html-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
1/9/2008 10:29:15 AM
|
|
On Jan 8, 8:53 pm, Philipp <sicsic...@freesurf.ch> wrote:> Hello,> I have the following two interfaces.>> //-----------------------> public class B extends A>> //-----------------------> public interface IA {> public A getFormat();>> public interface IB extends IA {> public B getFormat();> }}>> //----------------------->> Here, interface IB overrides the getFormat() method of its> super-interface IA. Instead of returning an A it now returns a B. Note> that B extends A.>> In my eclipse, if I choose a compiler compliance level <= Java 1.4 this> gives an error "The return type is incompatible with IA.getFormat()". If> I choose a compliance level >= 5.0 it is OK without problem.>> Is the overriding mechanism really different between Java 1.4 and 5?> (couldn't find any info about this on the new features page of J2SE 5.0)>> What can I do to still use this construct on a Java 1.3 compatible JVM?>> Thanks> PhilThis feature is introduce in J2SE 5.0, in which you can override themethod with the covariant return type. this covariant term is used inJ2SE 5.0 document. if i say in broad term then you can use any classthat is in class hierarchy.means any sup class or class specified inreturn type. internally it do implicit type conversion for you.
|
|
0
|
|
|
|
Reply
|
Neo_it
|
1/9/2008 10:30:15 AM
|
|
Philipp wrote:> Lew wrote:>> Andreas Leitgeb wrote:>>> Philipp <sicsicsic@freesurf.ch> wrote:>>>> public interface IA {>>>> public A getFormat();>>>> public interface IB extends IA {>>>> public B getFormat();>>>> }>>>> }>>>> What can I do to still use this construct on a Java 1.3 compatible JVM?>>>>>> Change interface IB's method to also return A. Sorry, no other way. >>> (any class that implements IB will>>> of course still return B objects at runtime, but they>>> can't state this fact such that old JVM knows.)>>>> This is called "covariant return", and was introduced in 1.5. > > OK. I didn't know the correct term. This use of the word "covariant" > somewhat differs from the mathematical sense...I sat on a bus talking to another programmer once. After ten minutes, another lady broke in and said, "I know every single word you guys have been using, but I have no idea what you've been talking about."Programming uses terms that differ from their English sense. Let alone their mathematical.For another programming term whose meaning differs from the mathematical, check out "idempotent".-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
1/9/2008 2:09:57 PM
|
|
Neo_it wrote:> This feature is introduce in J2SE 5.0, in which you can override the> method with the covariant return type. this covariant term is used in> J2SE 5.0 document. if i say in broad term then you can use any class> that is in class hierarchy.means any sup class or class specified in> return type. internally it do implicit type conversion for you.Here's why it's allowed. Consider public interface StatementCreator { java.sql.Statement createStatement() throws java.sql.SQLException; }and an implementing class with public com.lewscanon.sql.Statement createStatement() throws java.sql.SQLExceptionwhere the return type implements java.sql.Statement. From the point of view of any interface-typed variable that invokes the method, it is getting a java.sql.Statement back, so the contract is fulfilled. It's a natural concomitant of implicit widening conversions. As long as what you want is wider than what you got, you're fine.The put side is the converse. What you want to put has to be narrower, so the method signature of the callee has to be wider. This is "contravariant", or supertype substitution, contrasted with the "covariant" or subtype substitution in the return part of the method signature. This is where the concept of method overloads and the rules for finding the narrowest available signature, i.e., contravariant type pattern apply.The terms "contravariant" and "covariant" are more common in the context of generics, where the wildcard "? extends" is a covariant type, and "? super" is the contravariant. You see the latter a lot in Comparables and the like: public static <T extends Comparable<? super T>> void sort(List<T> list)<http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)> public static <T> void sort(List<T> list, Comparator<? super T> c)<http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)>-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
1/9/2008 2:39:55 PM
|
|
|
9 Replies
109 Views
(page loaded in 0.136 seconds)
Similiar Articles: Make a simple 4 row by 4 column scratch pad. - comp.lang.java.help ...... com The major difference between a ... Override public ... Make a simple 4 row by 4 column scratch pad. - comp.lang.java.help ... JTable and input methods ... Java Collections List : Converting from List '... select; } @Override public java ... The difference between 'String1' and 'String' is ... List : Converting from ... java.util.Iterator<S ... methods to ... Timer that Launches a Method at Regular Intervals - comp.os.ms ...In Java one can set up a timer, and using that timer arrange for methods to be called at regular intervals, say every five seconds. ... Difference between Search and Poll ... How to set a format in JFormattedTextField? - comp.lang.java ...I entered -1.2, 3.5 or "hello world", theyall ... custom action on an invalid input, try override AbstractFoamatter methods. ... in JFormattedTextField? - comp.lang.java ... I ... java in the database (10gR2 & 11gR2) - comp.databases.oracle ...... lets call it jc) which provides methods for ... Are there any > difference between 10g and 11g in this ... upgrade from oracle RAC 10.2.0.4 to 11.2.0.1 - comp ... java in the ... Problems to calculate sin - comp.lang.java.programmer... an error in the computation of the sine, it represents the difference between Java ... "Therefore, most methods with more than 0.5 ulp errors are required to be semi ... SPEED TEST: DirectX9 vs openGL - comp.graphics.api.opengl ...Drivers for 1.4 don't exist for each ... into specific relief, DirectX's overriding ... nvidia.com/object/nvtristrip_v1_1.html > or else one wouldn't notice a difference between ... conventional wisdom how to upsample very large arrays, accurately ...Hello all, I'd like to know what methods are ... The performance difference between OLA and OLS is small enough ... create animated picture sequence - comp.lang.java ... PHP strict standards? - comp.lang.phpThey tend to use languages like PHP, Java ... to still leave the standard ... for differences between ... topic - Strict standards : Non-static methods ... Installation 1.5 ... Preventing A New Window From Grabbing Focus - comp.lang.java ...... testing but my environment set in Eclipse for 1.4 ... you have or a difference in OS.> > This ... difference between ... instead in a new window ... methods for grabbing ... what is the difference between method overloading and method ...what is the difference between method overloading and method overriding and what is permittable ... in overloading and overriding of methods? ... networking in java (1) ... Method Overloading vs Method Overriding in Java with ExampleIn this article we will see difference between method overloading and overriding in java. ... handling for overloading and overriding methods in java ... 7/11/2012 11:20:37 PM
|