I was wondering, when creating new Activity
classes and then overriding the onCreate()
method, in eclipse I always get auto added: super.onCr
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
Eclipse is just being helpful, reminding you that you can call the superclass implementation if you want.
you are probably getting an error because you are not doing something necessary that the superclass does, since you are not calling its implementation.
If you want to make absolutely sure that the superclass-method is called as well, you must trick a bit: Don't allow the superclass-method to be overwritten, but have it call an overridable protected method.
class Super
{
public final void foo() {
foo_stuff();
impl_stuff();
}
protected void impl_stuff() {
some_stuff_that_you_can_override();
}
}
class Base extends Super
{
protected void impl_stuff() {
my_own_idea_of_impl();
}
}
That way, the user must call Super.foo() or Base.foo() and it will always be the base-class version as it was declared as final. The implementation-specific stuff is in impl_stuff(), which can be overriden.
If you want to force subclasses to execute the parent class' logic, a common pattern is something like the following:
public abstract class SuperClass implements SomeInterface
{
// This is the implementation of the interface method
// Note it's final so it can't be overridden
public final Object onCreate()
{
// Hence any logic right here always gets run
// INSERT LOGIC
return doOnCreate();
// If you wanted you could instead create a reference to the
// object returned from the subclass, and then do some
// post-processing logic here
}
protected abstract Object doOnCreate();
}
public class Concrete extends SuperClass
{
@Override
protected Object doOnCreate()
{
// Here's where the concrete class gets to actually do
// its onCreate() logic, but it can't stop the parent
// class' bit from running first
return "Hi";
}
}
This doesn't actually answer your question about what prompts Eclipse to automatically insert a superclass call into the implementation; but then I don't think that's the way to go anyway as this can always be deleted.
You can't actually enforce that a method must call the superclass' version with a Java keyword, or anything like that. I suspect that your exceptions simply came from some code in the parent class checking expected invariants, or something, that were invalidated by your approach. Note that this is subtly different from throwing an exception because you failed to call super.onCreate()
.
There is nothing in Java that forces calling of super, and there are plenty of examples when you wouldn't want to. The only place where you can force calling of super is in constructors. All constructors have to call a superclass constructor. One (the no arguments constructor) will be inserted if you don't write one explicitly, and if there is no no-arguments constructor then you must call it explicitly.
Eclipse just helps you doing things right and avoid exceptions.
From http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.