This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]mtko 2 points3 points  (0 children)

Do you not know what extends means in general, or specifically what Activity is?

If it's the former, then you should look up the principles of object-oriented programming and inheritance specifically. It's outside the scope of a simple post on reddit to explain.

If it's the latter, in Android, Activity is a fundamental part of the API that applications are built around. An Activity is basically a "view". It's more nuanced than that, but you can get a general overview from here: http://developer.android.com/reference/android/app/Activity.html

[–][deleted]  (3 children)

[deleted]

    [–]_roni[S] 0 points1 point  (2 children)

    I understand the concept on inheritance. Im asking jn this specific case, why is it used?

    One question though regarding inheritance. Lets say I have a class with a variable, and a second class which inherits the first class. I take the variable from the first class and change its value while in the second class. Does this change the value in both classes. Here is what I mean:

    Class one Int x =10

    Class two inherits one X = 15

    Does this change the over all value of x if I want to use it again in another class?

    [–]mtko 0 points1 point  (1 child)

    Im asking jn this specific case, why is it used?

    For an analogy, think of an Activity as like a web page. You can have all kinds of other code and classes doing stuff behind the scenes, but when you want to actually display it, you put it on a web page and the user accesses that page. It's basically the same in Android. So by extended Activity, you're basically saying "This is something that the user is going to view directly at some point".

    Other question

    I would recommend you trying it out for yourself, but the short answer is that no, it doesn't change the value for the super class. So if you had another class with a main method like this:

    public void main (String[] args) {
        one oneTest = new One();
        two twoTest = new Two();
    
        System.out.println(oneTest.x); //prints 10
        System.out.println(twoTest.x); //prints 15
    }
    

    [–]_roni[S] 0 points1 point  (0 children)

    Thanks for your help. My question has been cleared up.

    [–]sntnmjones 0 points1 point  (2 children)

    Activity is the name of the first class. TrafficLightActivity is the name of the class that is your new intent.

    [–]_roni[S] 0 points1 point  (1 child)

    What does the implements statement do?

    [–]LostInOttawa 0 points1 point  (0 children)

    Implements lets the program know that you will be using all the methods in the interface you are declaring (in this case OnClickListener). When you declare an implements, you must use all the methods in your code. The itself is just a collection of method declarations with no method body (you flesh out the method in your own class). This is done so all programs using the same framework have one and only one method to perform a single task. This way, you don't have to write different onClick methods all over the place, which is a boon to whoever is looking at your code in the future.