5

I read some documentation of it and realized that there appears to be a relationship between @override in Java and @decorator in Python.

Can someone explain the relationship in plain English?

I understand that functions in Python are first class object and functions can be parameter of another function.

How does this @override syntax in Java differ from decorators in Python?

JSK NS
  • 3,346
  • 2
  • 25
  • 42
user1406647
  • 489
  • 1
  • 5
  • 17
  • 1
    possible duplicate of [What's "@Override" there for in java?](http://stackoverflow.com/questions/2489974/whats-override-there-for-in-java) – Martijn Pieters Mar 18 '14 at 09:46
  • 1
    Java `@Override` is a compiler annotation. Apart from a vague resemblance to the Python `@decorator` syntax, there is **no** other correlation between the two concepts. – Martijn Pieters Mar 18 '14 at 09:47

1 Answers1

18

There is no relation between @override in Java and @decorator in Python.

@override in Java is an annotation which marks a method as overwriting another method. So when I extend a base class and I overwrite some method of it, I can annotate this method with @override. This has no real effect on my code, it is just a hint for the compiler. Like Martijn Pieters suggests, see What's "@Override" there for in java? for more.

@decorator in Python is a design pattern. A decorator can be attached to a method to extend its functionality. If a method is called, its decorators will be called before. You can use this for example to log information. I used it once in a project to enable caching.

The most comparable thing to a @decorator in Java would be to define an annotation which can act like a decorator or use a library like AspectJ. However, this is not part of the core Java language which simply doesn't know any decorator annotation. A decorator and an annotation are two different things.

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80