4
package com.example.mapdemo;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.mapdemo.view.FeatureView;

/**
 * The main activity of the API library demo gallery.
 * <p>
 * The main layout lists the demonstrated features, with buttons to launch them.
 */
public final class MainActivity extends ListActivity {

    /**
     * A simple POJO that holds the details about the demo that are used by the List Adapter.
     */
    private static class DemoDetails {
        /**
         * The resource id of the title of the demo.
         */
        private final int titleId;

        /**
         * The resources id of the description of the demo.
         */
        private final int descriptionId;

        /**
         * The demo activity's class.
         */
        private final Class<? extends FragmentActivity> activityClass;

        public DemoDetails(
                int titleId, int descriptionId, Class<? extends FragmentActivity> activityClass) {
            super();
            this.titleId = titleId;
            this.descriptionId = descriptionId;
            this.activityClass = activityClass;
        }
    }

There are two things that I find I need a better understanding of when it comes to this code. First I am trying to determine the exact meaning of:

private final Class<? extends FragmentActivity> activityClass;

Secondly I am curious what is being called with super(); since when DemoDetails is defined it is defined with:

private static class DemoDetails {

So at that point there is no extends so what is being referenced by super? The use of

<? extends >

I have not ever seen before.

Thanks in advance...

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Eae
  • 4,191
  • 15
  • 55
  • 92
  • all classes in Java are derived from `Object` so in this case `super()` is obsolete and this line may be there because the constructor was generated. – Marco Forberg Jun 03 '13 at 06:49
  • @MarcoForberg: if `super()` weren't called explicitly here, it would have been called implicitly anyway. – jlordo Jun 03 '13 at 06:53
  • @MarcoForberg: I am quite sure ;) See the Java Language Specification, Section 12.5 Creation of New Class Instances Point 3, [Link here](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5) – jlordo Jun 03 '13 at 06:55
  • okay learnt something today, ty ;) – Marco Forberg Jun 03 '13 at 07:01

7 Answers7

2
private final Class<? extends FragmentActivity> activityClass;

That declares a field named activityClass which is private (visible only within the class where it's declared) and final (no re-assingment). It's type is Class<? extends FragmentActivity> (which is a parameterized type or an (type-) instantiation of the generic type Class<T>, where T is a type parameter). When generic types are used they are called parameterized type and provide a type argument for the type parameter. Here the type argument is ? extends FragmentActivity (which actually is an wildcard with an upper bound, and the upper bound here happens to be FragmentActivity.

This means activityClass can refer to a Class instance which represents either the class FragmentActivity itself or any of it's subclasses (e.g. FragmentActivity.class).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

private- that means this variable cannot be accessed outside this class final- once the value is assigned it cannot be changed.

So a private final Class is a Class that can only be accessed in this file and cannot be assigned a new value- its a constant.

super- every class extends Object. Only Object has no parent in the hierarchy. So super() is calling Object(). It's not needed, probably either added by someone who didn't know that, or the class used to have a parent and was refactored but that line was missed.

<? Extends FragmentActivity>- this means the class is a generic. Some of its functions can work on a large set of objects, and we'll specify that when we instantiate the class. For now it's a ?. The Extends FragmentActivity part means only classes that extend FragmentActivity are valid values of ?. The obvious question is then why not just make it take a FragmentActivity? Using a generic restricts all of the ? to be the same type, whereas taking the base class could allow them to be mixed types- the ? is more specific.

EDIT: originally missed a capital and put the meaning of final for classes, not Classes. Fixed.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

To understand the statement

private final Class<? extends FragmentActivity> activityClass;

and the corresponding constructor parameter, remember that Object has a getClass() method:

public final Class<?> getClass();

Also, if you have a named type (without anonymous subclassing), then you can use .class:

String s = "a string";
assert s.getClass().equals(String.class);
// String.class is a Class<String> object.

There are a number of places where you use this mechanism. Reflection is one, though rare. Log4J loggers is another.

The safest way to declare a class of an arbitrary object is:

Thing t = ...;  // assume not null.
Class<? extends Thing> clazz = t.getClass();

After all, t might be an instance of a subclass of Thing, and perhaps even an anonymous subclass. This expression means that clazz is a Class object, and the specific type variable of clazz is some, not-necessarily known, subclass of Thing, or maybe just Thing itself.

Back to the original expression, activityClass is a class object for FragmentActivity or some subtype of it. And the program can construct an instance of it with Class.newInstance() if it has a 0-argument constructor.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0
private final Class activityClass;

Will stop other classes from extending your class. You can also do this with methods so that your methods will not be overridden. http://docs.oracle.com/javase/tutorial/java/IandI/final.html provides more information

super();

Will call the parent's constructor (in this case being the ListActivity that you extended) http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Nicholas
  • 723
  • 5
  • 11
  • The super() is called in the constructor of the inner class. It has nothing to do with the ListActivity. – britzl Jun 03 '13 at 06:59
0

Ok, let's get to it:

private final Class<? extends FragmentActivity> activityClass;

The DemoDetails class takes a Class as a constructor argument. This class must be of type FragmentActivity or a subclass thereof. There are several uses of passing a Class reference, one is that you can create new instances using activityClass.newInstance(). Another is to do runtime checks of types using isAssignableFrom()

Secondly I am curious what is being called with super()

This has absolutely no effect. There's nothing going on in the super-class (Object), but it's a good idea to always call super(). Especially if you at a later stage decide to let DemoDetails inherit from something else.

Community
  • 1
  • 1
britzl
  • 10,132
  • 7
  • 41
  • 38
0
 private final Class<? extends FragmentActivity> activityClass;

The meaning of this line is activityClass is a private and final which can hold the reference variable of FragmentActivity or all the sub-class of FragmentActivity only.

super(); 

This will call the constructor of the super class i.e ListActivity.If you don't write this code,compiler will automatically append this code in .class file.

 <? extends temp>

This measns,this container can hold the reference variable of temp or a sub-class of temp only.If one try to pass other ref.var,a compile time error occur.So,it is basically to restrict the type of reference variable to hold.

Nirdesh Sharma
  • 734
  • 5
  • 14
0

Question 1.there is no extends so what is being referenced by super?

Ans:- Every class is subclass of Class Object in java by default.So Object class empty constructor is called first then subclass constructor.

Question 2.private final Class activity Class meaning?

Ans:- A class that is declared final cannot be subclassed.private class is a class that can be accessed within same class file and not by other class file in same or different package.

what is this <? extends >

Ans-it is implemention of generalization in java.same as template in c++.

Sharad Mhaske
  • 1,103
  • 9
  • 19