Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of constructor is this, beginning with <T extends Drawable & DrawerToggle>? [duplicate]

<T extends Drawable & DrawerToggle> ActionBarDrawerToggle(
        Activity activity, 
        Toolbar toolbar,
        DrawerLayout drawerLayout, 
        T slider,
        @StringRes int openDrawerContentDescRes,
        @StringRes int closeDrawerContentDescRes) {

During browsing source code of the class ActionBarDrawerToggle.java, I've found this constructor is declared without access specifier. Rather, its declaration starts with

<T extends Drawable & DrawerToggle>

Please explain, what does it really mean?

like image 864
Vikas Patidar Avatar asked Feb 05 '16 11:02

Vikas Patidar


People also ask

What is drawable in Java?

In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn: The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be.

What is drawable?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is v24 in Android drawable?

Classic drawable resources such as images are stored in the drawable folder. In contrast, vector drawables are stored in drawable-v24 . For this project, keep the drawable default and click OK. You should now see the New File dialog box.

Which of the following ways defines and instantiates the drawable class?

There are two ways to define and instantiate a Drawable besides using the class constructors: Inflate an image resource (a bitmap file) saved in your project. Inflate an XML resource that defines the drawable properties.


2 Answers

T is generic type declaration which is then used as a type for one of the arguments - slider.

<T extends Drawable & DrawerToggle > specifically means that T must extend/implement both Drawable and DrawerToggle classes/interfaces.

The access specifier is not mandatory. In case it's missing it means the class/method is accessible only from classes within the same package.

like image 56
Zbynek Vyskovsky - kvr000 Avatar answered Oct 12 '22 16:10

Zbynek Vyskovsky - kvr000


Like normal methods, constructors can take type parameters. This is mentioned in this section of the Java Language Specification:

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8

From the syntax mentioned there, you can have type parameters in the signature:

TypeParameters:

< TypeParameterList >

TypeParameterList:

TypeParameter {, TypeParameter}

and where TypeParameter is specified as

TypeParameter:

{TypeParameterModifier} Identifier [TypeBound]

TypeParameterModifier:

Annotation

TypeBound:

extends TypeVariable

extends ClassOrInterfaceType {AdditionalBound}

AdditionalBound:

& InterfaceType

like image 29
M A Avatar answered Oct 12 '22 15:10

M A