Using super (); in java

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 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 to better understand when it comes to this code. First I try to determine the exact meaning:

 private final Class<? extends FragmentActivity> activityClass; 

Secondly, I'm curious what is called super (); because when DemoDetails is defined, it is defined using:

 private static class DemoDetails { 

So, at this moment there are no extensions, so what does super refer to? Using

 <? extends > 

I have never seen it before.

Thanks in advance...

+4
source share
7 answers
 private final Class<? extends FragmentActivity> activityClass; 

Declares a field named activityClass , which is private (visible only inside the class where it was declared), and final (without recounting). Class<? extends FragmentActivity> it type Class<? extends FragmentActivity> Class<? extends FragmentActivity> (which is a parameterized type or (type-) instance of the generic type Class<T> , where T is a type parameter). When generic types are used, they are called a parameterized type and provide a type argument for the type parameter. Is there an argument of type ? extends FragmentActivity ? extends FragmentActivity (which is actually a wildcard with an upper bound , and the upper bound is FragmentActivity .

This means that activityClass can refer to an instance of Class , which is the FragmentActivity class itself or any of its subclasses (for example, FragmentActivity.class ).

+2
source

private - this means that this variable cannot be accessed outside of this final class - as soon as a value is assigned, it cannot be changed.

Thus, a private final class is a class that can only be accessed in this file, and it cannot be assigned a new value - its constant.

super - each class extends Object. Only an object does not have a parent in the hierarchy. Therefore, super () calls Object (). It is not needed, perhaps either added by someone who did not know this, or by the class used for the parent, and was refactored, but this line was skipped.

<? Extends FragmentActivity> <? Extends FragmentActivity> - this means that the class is shared. Some of its functions can work with a large set of objects, and we will indicate that when creating an instance of the class. Is this at the moment ?. The Extends FragmentActivity part means that only classes that extend FragmentActivity are valid values ​​?. The obvious question is, why not just do a FragmentActivity? Using generic limits everything? be the same type, while taking a base class can allow them to be mixed types? more specific.

EDIT: initially skipped capital and set the value to final for classes, not classes. Fixed.

+1
source

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(); 

Alternatively, if you have a named type (without an anonymous subclass), you can use .class :

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

There are several places where you use this mechanism. Reflection is one, albeit rare. Log4J loggers is different.

The safest way to declare an arbitrary object class is:

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

After all, t may be an instance of the Thing subclass, and possibly even an anonymous subclass. This expression means that clazz is a Class object, and a particular variable of type clazz is some, not necessarily known, subclass of Thing or maybe only Thing .

Returning to the original expression, activityClass is a class object for a FragmentActivity or some subtype. And a program can build an instance using Class.newInstance() if it has a constructor with 0 arguments.

+1
source
 private final Class activityClass; 

Let other classes extend your class. You can also do this with methods so that your methods are not overridden. http://docs.oracle.com/javase/tutorial/java/IandI/final.html provides additional information

 super(); 

Call the parent constructor (in this case, an extended ListActivity) http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

0
source

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 its subclass. There are several ways to use the Class reference, one of which is that you can create new instances using activityClass.newInstance () . Another is to perform type-time checks using isAssignableFrom ()

Secondly, I'm curious what is called using super ()

This has no effect. Nothing happens in the superclass (Object), but it is a good idea to always call super (). Especially if at a later stage you decide to let DemoDetails inherit something else.

0
source
  private final Class<? extends FragmentActivity> activityClass; 

The value of this line is: activityClass is private and final, which may contain the reference variable FragmentActivity or the entire subclass of FragmentActivity.

 super(); 

This will call the constructor of the superclass ie ListActivity.If you will not write this code, the compiler will automatically add this code to the .class file.

  <? extends temp> 

In this container, this container may contain the temp reference variable or a temp time subclass. If you try to pass another ref.var, a compile-time error occurs. So basically this is a control variable type constraint for storage.

0
source

Question 1. There are no extensions, so what does super refer to?

Ans: - Each class is a subclass of the Object class in java by default . The constructor of the Object class classes is first called, and then the subclass constructor.

Question 2.private final Class activity Class value?

Ans: - The class declared final cannot be a subclass of .private class - it is a class that can be accessed in the same class file and not by another class file in the same or different package.

what is this <? extends > <? extends >

Ans is an implementation of generalization in java.same as a template in C ++.

0
source

Source: https://habr.com/ru/post/1484140/


All Articles