Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @override in Java and @decorator in Python

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?

like image 719
user1406647 Avatar asked Mar 18 '14 09:03

user1406647


Video Answer


1 Answers

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.

like image 114
Thomas Uhrig Avatar answered Sep 24 '22 17:09

Thomas Uhrig