Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overriding

Tags:

java

Can you override method in java without using annotations? Because eclipse doesn't support it if you use JRE6, you need to switch back to 5 to use @Override annotation. Will this method be overriden if I remove the annotation?

@Override public String toString() {
    return name + " [" + genre + "]";
    }
like image 786
Gandalf StormCrow Avatar asked Mar 22 '10 09:03

Gandalf StormCrow


People also ask

What is method overriding explain with example?

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

What is method overriding and method overloading?

Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters. Method Overriding is a mechanism to achieve polymorphism where the super class and the sub-class have same methods, including the parameters and signature.

Why method overriding is used?

The purpose of Method Overriding is that if the derived class wants to give its own implementation it can give by overriding the method of the parent class. When we call this overridden method, it will execute the method of the child class, not the parent class.

What are the rules for method overriding?

Rules For Method OverridingA static method cannot be overridden. Private methods cannot be overridden. The return type of the overriding method must be the same. We can call the parent class method in the overriding method using the super keyword.


2 Answers

Can you override method in java without using annotations?

Yes, the @Override annotation is not required to override a method.

Just by the act of having a method definition with the same method signature of an ancestor class is enough to override a method.

The @Override annotation is just a reminder to the compiler that says that the method definition is intended to override a method.

If a method that has a @Override does not actually override a method, the compiler will throw a compiler error -- the annotation serves as an additional compile-time check to see whether the method definition does indeed override a method.

Because eclipse doesn't support it if you use JRE6, you need to switch back to 5 to use @Override annotation.

I happen to use Eclipse targeting Java 6, and am able to use the @Override annotation, so I'm not quite sure what can be causing this. Annotations has been supported from Java 5, and has been more tightly integrated into the language since Java 6.

Edit

From Andreas_D's comment to the question:

smile - this just indicates, that the current source level for your project is < 1.5. This setting is pretty independant from the JRE, you actually can use JRE 1.6 together with source level 1.4 - and I guess, that's what happened.

This sure seems to be one way a compile error can occur!

The way to check if the "compiler compliance level" is set to something under Java 5 is,

  1. Go to the Properties of the Java project in Eclipse
  2. Then, go to the Java Compiler menu
  3. Check that the Compiler Compliance Level is set to 1.5 or higher.

Edit

Regarding to Gandalf StormCrow's screenshot, appears that the compiler compliance level is set lower than JDK 1.5. Clicking on the Change project compliance and JRE to 1.5 quick fix should fix up the current situation.

The issue is, annotations were introduced in Java 5.

It appears that the Java project in Eclipse is currently set so the source code can only contain features which were introduced prior to Java 5, therefore disallowing the use of annotations.

The way to fix the situation is tell Eclipse that features from Java 5 can be used, by setting the compiler compliance level to 1.5 or higher.

like image 114
coobird Avatar answered Nov 09 '22 15:11

coobird


@override is just a way of ensuring that if you change the contract (the method overriden) this method will not compile. Without it, it will be considered as a different fonction.

As for the JRE6, i'm not sure what you mean : annotations are supported.

Try the jdk ?

like image 41
Maxime ARNSTAMM Avatar answered Nov 09 '22 15:11

Maxime ARNSTAMM