Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noob question about "Hello World" Android tutorial

Tags:

android

Just getting started with Android development and Java. So, here's the code I'm working with:

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

What is the purpose of declaring the onCreate() method here:

public void onCreate(Bundle savedInstanceState) {

Then using super to call the onCreate() method here:

super.onCreate(savedInstanceState);

Doesn't this mean that you are calling the onCreate() method from the Activity class rather than the HelloAndroidActivity class? If so, what is the point of declaring a method with the same name in the HelloAndroidActivity class?

Thanks for any clarification.

like image 769
Axl Avatar asked Nov 30 '22 07:11

Axl


2 Answers

In your example, HelloAndroidActivity is a class that inherits from Activity. You are then overriding the base class (Activity)'s onCreate method. Overriding occurs when you define a method in a derived class with the same signature as an existing method in the base class. This means that when I call onCreate on an instance of HelloAndroidActivity, I will execute the HelloAndroidActivity version, and not the base class (Activity)'s version.

The instruction super.OnCreate(savedInstanceState) in the overridden version is explicitly calling the base class's version of the method. What this means is you want HelloAndroidActivity.onCreate to first execute the base class's implementation, then run some more code.


Take the following examples to illustrate this behavior (assume the method Output just outputs something to the screen):

1.

class A
{
    public void DoSomething()
    {
        Output("A");
    }
}

In this case, calling A.DoSomething() will output "A".


2.

Assume we still have the class A defined as above, and the following:

class B extends A
{
}

In this case, calling B.DoSomething() will also output "A".


3.

Assume we still have the class A defined as above, and the following instead:

class B extends A
{
    @Override
    public void DoSomething()
    {
        Output("B");
    }
}

Now, calling B.DoSomething() will output "B".


4.

Assume we still have the class A defined as above, and now this instead:

class B extends A
{
    @Override
    public void DoSomething()
    {
        super.DoSomething();
        Output("B");
    }
}

Now, calling B.DoSomething() will output "A" and then "B".

like image 129
lc. Avatar answered Dec 12 '22 05:12

lc.


You are creating a class that inherits from Activity; but you have to define its behaviour, so you tell your class, when created, to call base class to complete all the things that must be done (super.onCreate()), then you set your layout to show your screen/app (setContentView()).

One thing more: take a look at @Override defined right before public void onCreate(Bundle savedInstanceState): override means that you're extending base method to let your derived class do all the work of the base one, including (after) your logic.

like image 39
Marco Avatar answered Dec 12 '22 05:12

Marco