Вопрос

I'm new to Android development and Java. I dont understand what this line of code actually means and what's it's significance... PLease help...

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
Это было полезно?

Решение

This means, onCreate is a method in super class, that is the class which you current class is extending.

@Override is an annotation, which ensures that onCreate is in super class, otherwise compiler will complain. This annotation ensures that you don't mess up syntax of the method and at runtime struggle to find where the problem is.

super.onCreate(savedInstanceState);

This statement calls super class onCreate first and then continues.

@Override explained here

class SuperClass
{
    public void onCreate()
    {
    System.out.println("Super");
    }
}

public class Apps extends SuperClass
{
    @Override
    public void onCreate()
    {
    super.onCreate();
    System.out.println("Sub");
    }

    public static void main(String[] args)
    {
    SuperClass supRef = new Apps();
    supRef.onCreate();
    }

}

Now, Apps is a sub class and SuperClass is the class which is extended by Apps, so this is super class.

Now, further output of this program will be :

Super
Sub

Hope this explains call to super.onCreate();

Now suppose you define onCreate in Apps as :

public void onCreate(int h)

without @Override compiler will not complain, and output will be

Super

So you will have to recheck you code, or debug why Sub is not printed.

And one last thing, these are basics of OOPs and Java, I think you should read about these concepts first before start coding in Java.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top