Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force derived class to call super method? (Like Android does)

I was wondering, when creating new Activity classes and then overriding the onCreate() method, in eclipse I always get auto added: super.onCreate(). How does this happen? Is there a java keyword in the abstract or parent class that forces this?

I don't know if it is illegal not to call the super class, but I remember in some methods that I got a exception thrown for not doing this. Is this also built-in into java? Can you use some keyword to do that? Or how is it done?

like image 702
Peterdk Avatar asked Nov 18 '10 16:11

Peterdk


People also ask

How do you call a super class method?

If you want the superclass constructor called with specific arguments, explicitly call the superclass constructor from the subclass constructor. The call to the superclass constructor must come before any other references to the object.

Can override method call super?

A subclass can override methods of its superclass, substituting its own implementation of the method for the superclass's implementation.

How do you call the super class method in subclass?

Subclass Constructors Invocation of a superclass constructor must be the first line in the subclass constructor. super(); or: super(parameter list);

Can we call super in method?

Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class. Super keyword are not used in static Method.


1 Answers

This is added in the support annotation library:

dependencies {     compile 'com.android.support:support-annotations:22.2.0' } 

http://tools.android.com/tech-docs/support-annotations

@CallSuper

like image 115
runor49 Avatar answered Oct 09 '22 07:10

runor49