@override annotation not working correctly javac version _ 1.7.0_25

Y

Y HA

Hi!

This code should output:

Run method overridden in Task class
overridden method run() in PeriodicTask class


but I'm having this :

# javac -version
javac 1.7.0_25

# javac CollectionTest.java
CollectionTest.java:18: error: incompatible types
@Override
^
required: Annotation
found: Override
CollectionTest.java:27: error: incompatible types
@Override
^
required: Annotation
found: Override
2 errors



/**
* Java program to demonstrate how to override method in Java.
* Overridden method are resolved during runtime based upon type of object
* @author Javin
*/
public class CollectionTest {

public static void main(String args[]) {
Runnable task = new Task();
task.run(); //call overridden method in Task
task = new PeriodicTask();
task.run(); //calls overridden method in PeriodicTas
}


}

class Task implements Runnable{
@Override
public void run() {
System.out.println("Run method overridden in Task class");
}
}

class PeriodicTask extends Task{
@Override
public void run() {
System.err.println("overridden method run() in PeriodicTask class");
}
}
 
E

Eric Sosman

Hi!

This code should output:

Run method overridden in Task class
overridden method run() in PeriodicTask class


but I'm having this :

# javac -version
javac 1.7.0_25

# javac CollectionTest.java
CollectionTest.java:18: error: incompatible types
@Override
^
required: Annotation
found: Override
CollectionTest.java:27: error: incompatible types
@Override
^
required: Annotation
found: Override
2 errors



/**
* Java program to demonstrate how to override method in Java.
* Overridden method are resolved during runtime based upon type of object
* @author Javin
*/
public class CollectionTest {

public static void main(String args[]) {
Runnable task = new Task();
task.run(); //call overridden method in Task
task = new PeriodicTask();
task.run(); //calls overridden method in PeriodicTas
}


}

class Task implements Runnable{
@Override
public void run() {
System.out.println("Run method overridden in Task class");
}
}

class PeriodicTask extends Task{
@Override
public void run() {
System.err.println("overridden method run() in PeriodicTask class");
}
}

It works fine for me, with javac 1.7.0_45 (slightly newer
than yours). Just a wild guess: Does your source file contain
any strange, stray, invisible characters -- NUL's, maybe --
that confuse the compiler but disappear when posted as a Usenet
message? Try copying the content of your message and compiling
that; if you get a different outcome it may be time to use a hex
viewer on both files.
 
Y

Y HA

* created a new file with vi and pasted the code from the web.
* updated SDK and RE to 1.7.0_45

still happens the same
 
E

Eric Sosman

* created a new file with vi and pasted the code from the web.
* updated SDK and RE to 1.7.0_45

still happens the same

Then I'm at a loss. File with strange characters? Unlikely,
if copy/paste from the web (which worked fine for me) still fails
for you. Corrupted Java installation? Again unlikely, since
you've re-installed.

Have you done something to anger the evil spirits? :-(

(In other words: I have no idea what's wrong. Sorry!)
 
J

John B. Matthews

/**
* Java program to demonstrate how to override method in Java.
* Overridden method are resolved during runtime based upon type of object
* @author Javin
*/
public class CollectionTest {

public static void main(String args[]) {
Runnable task = new Task();
task.run(); //call overridden method in Task
task = new PeriodicTask();
task.run(); //calls overridden method in PeriodicTas
}


}

class Task implements Runnable{
@Override
public void run() {
System.out.println("Run method overridden in Task class");
}
}

class PeriodicTask extends Task{
@Override
public void run() {
System.err.println("overridden method run() in PeriodicTask class");
}
}

For reference,

$ props.sh | grep "os\."
os.arch: x86_64
os.name: Mac OS X
os.version: 10.9

$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

$ java -cp build/classes CollectionTest
Run method overridden in Task class
overridden method run() in PeriodicTask class
 
F

Fredrik Jonson

Y said:
class Task implements Runnable {
@Override
public void run() {
System.out.println("Run method overridden in Task class");
}
}

Sorry, I can't help with the problem, but the code made me curious.

Is it commont practice to use @Override when implementing interface
methods? Assuming that the method has not been implemented by a
superclass.

Obviously, the annotation is technically allowed, still it isn't really
possible to override an implementation that does not exist. And it
doesn't add type safety to the code, if your method signature doesn't
match the method signature of the interface, the implementing class
won't compile.
 
S

Silvio

Sorry, I can't help with the problem, but the code made me curious.

Is it commont practice to use @Override when implementing interface
methods? Assuming that the method has not been implemented by a
superclass.

Obviously, the annotation is technically allowed, still it isn't really
possible to override an implementation that does not exist. And it
doesn't add type safety to the code, if your method signature doesn't
match the method signature of the interface, the implementing class
won't compile.

It is an example of an annotation that should have been syntax and yes,
it is common practice. People use this to express that they did not mean
to come up with a new method of their own but they intend to override a
method from the base class. If no such method exists in an (indirect)
base class this will result in an error (or a warning, I forgot).
 
J

Jeff Higgins

Sorry, I can't help with the problem, but the code made me curious.

Is it commont practice to use @Override when implementing interface
methods? Assuming that the method has not been implemented by a
superclass.

Obviously, the annotation is technically allowed, still it isn't really
possible to override an implementation that does not exist. And it
doesn't add type safety to the code, if your method signature doesn't
match the method signature of the interface, the implementing class
won't compile.
Some interesting discussion around this Netbeans bug report.
<https://netbeans.org/bugzilla/show_bug.cgi?id=118928>
 
Y

Y HA

# javac CollectionTest.java
CollectionTest.java:14: error: incompatible types
@Override
^
required: Annotation
found: Override
CollectionTest.java:21: error: incompatible types
@Override
^
required: Annotation
found: Override
2 errors

# java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
 
Y

Y HA

the same happens with this code :



# javac MyMethod.java
MyMethod.java:12: error: incompatible types
@Override
^
required: Annotation
found: Override
1 error

#

public class MyMethod {

String str = "Philadephia";
public void engage(String str) {
System.out.println(str);
}
}


final class Override extends MyMethod {

@Override
public String engage() {
return this.str;

}

/*public void engage(String str) {
System.out.println(str + " " + str);
}
*/


public static void main(String[] args) {
Override sc = new Override();
//the method you intended to override:
//sc.engage("Philadephia");
System.out.println(sc.engage());
}
}
 
Y

Y HA

well, I'm picking up Java again

I don't have experience coding in Java, and found this code that supposedly runs with 1.7 but the same error happens with other @Override sample code...
 
M

markspace

In my experience, it's not _uncommon_, but I wouldn't say it's common
either.

It can be useful to have @Override in place on interface methods when
you're refactoring or otherwise modifying the interface being
implemented, in particular it helps to prevent dead code when you
delete methods from an interface.

Also, if the interface were less well known that Runnable, it would be
helpful for a maintainer to see which methods were overridden.

public class Fubar implements ArgleBargle {

public void doThing() {}

@Override
public void otherThing() {}

}

This makes one suspect that "doThing"is not part of the ArgleBargle
interface, but something new added in Fubar.
 
J

John B. Matthews

the same happens with this code : [...]
final class Override extends MyMethod {

@Override
public String engage() {
return this.str;
} [...]
}

In this example, the identifier Override used for the class name
collides with the same identifier used for the annotation name. You can
use the fully qualified name, @java.lang.Override, or rename the class.

public class MyMethod {

private static final String str = "Philadephia";

public void engage(String str) {
System.out.println(str);
}

private static final class MyOverride extends MyMethod {

@Override
public void engage(String str) {
super.engage(str);
}

public static void main(String[] args) {
MyOverride mo = new MyOverride();
mo.engage(str);
}
}
}
 
Y

Y HA

got this error with this code which also uses @Override

# javac MySuperClass.java
MySuperClass.java:11: error: incompatible types
@Override
^
required: Annotation
found: Override
1 error



public class MySuperClass {

public void doTheThing() {
System.out.println("Do the thing");
}
}


class MySubClass extends MySuperClass{

@Override
public void doTheThing() {
System.out.println("Do it differently");
}
}
 
J

John B. Matthews

got this error with this code which also uses @Override

# javac MySuperClass.java
MySuperClass.java:11: error: incompatible types
@Override
^
required: Annotation
found: Override
1 error



public class MySuperClass {

public void doTheThing() {
System.out.println("Do the thing");
}
}


class MySubClass extends MySuperClass{

@Override
public void doTheThing() {
System.out.println("Do it differently");
}
}

I see no error on the platform cited up-thread. You may have
adventitious classes in the classpath. Start with a clean build; failing
that, scrutinize your IDE's cache and your platform's java.ext.dirs.
 
K

Knute Johnson

I see no error on the platform cited up-thread. You may have
adventitious classes in the classpath. Start with a clean build; failing
that, scrutinize your IDE's cache and your platform's java.ext.dirs.

My mother always said if I hung out with the smart kids, I might just
learn some new vocabulary.


ad·ven·ti·tious
[ad-vuhn-tish-uhs] Show IPA
adjective
1.
associated with something by chance rather than as an integral part;
extrinsic.
 
V

Volker Borchert

Fredrik said:
Is it commont practice to use @Override when implementing interface
methods? Assuming that the method has not been implemented by a
superclass.

It helps reading the code of classes that have many methods, maybe
some of them with similar sounding names, but only fsw implementing
some not so well-known interface.

For that reason, I'd even like @Override to allow a value indicating
the class or interface where the overridden method lives. For now,
I'm (ab-)using @see javadoc for this purpose, but that isn't part of
the method's signature and therefore doesn't show up as prominently
in IDEs' code completion suggestions etc.
 
V

Volker Borchert

Wayne said:
Instead of that, why not create your own annotation, something like:

@From("MyInterface")
@Override
public void someMethod () { ... }

Too verbose, too non-standardized, and too error-prone. @Override gives
a compile-time error if the mothods actually doesn't override something.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top