0

I was just reading "Head First Android" and came across this code snippet,

public class WorkoutListFragment extends ListFragment{
    static interface WorkoutListListener{
        void itemClicked(long id);
    }
}

// Inside the same class there is this Inner Interface 
// declaration of the above Interface, 
// Declaration style : 01
private WorkoutListListener listener;

If i write like this below snippet is it better?

// why can't i declare the Inner Class like this in the same class,
// Because I write WorkoutListFragment.WorkoutListListener while implementing it,
// Declaration style : 02
private WorkoutListFragment.WorkoutListListener listener;

My query is which is the convention here, Declaration style 1 or 2? Because as I have seen here in StackOverflow that while implementing an Inner Interface we use the outer class or Interface name then give Dot and then write the Inner Interface name. like this,

public SomeClass implements SomeOuterClass.AnInnerInterface{
....
}

N.B: Please note, the comments are not in the actual code. I have added them for the clarity of my query.

2 Answers2

0

As you mentioned above, pattern you found is for communication between Fargment and Activity. For more information see here.

Declaration Style 1:

This pattern is for if you want to invoke any methods of your interface like,

listener.itemClicked(long id);

which will execute implemented method.

Declaration Style 2:

This patterns is for implementation of interface as below:

@Override
void itemClicked(long id){
// Add your code here
}

Thanks.

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • That means in the same class where I declare the InnerClass I should avoid declaration Style 2, "WorkoutListFragment.WorkoutListListener" and use this pattern only when using it outside of the defining Outer Class. – AbSak-The Humble Learner Jan 19 '17 at 05:45
  • @AbSak-The Humble Learner: If your requirement is to invoke method in same class then why to interface? You can just just create instance method and call it right? – AndiGeeky Jan 19 '17 at 05:53
0

What I got from your question is that you are also confused not with the convention but the way Interface is declared and used.

If we talk about the conventions, Yes the interfaces are used in the following way,

 public SomeClass implements SomeOuterClass.AnInnerInterface{
....
}

Because this is the way you are telling to "SomeClass" to implement the interface of "SomeOuterClass". It means that the "AnInnerInterFace" is the interface that exist in the "SomeOuterClass".

So this is the way you will use the public "AnInnerInterFace" and this is the way you will get access to it. and the "SomeClass" has to implements its method (which is declared in the interface namely "AnInnerInterface". You do not need to get its object here as you stated above

private WorkoutListFragment.WorkoutListListener listener;

This way is not good and of no use, Why would you do that in other class? interface needs to implement . I got no sense why you are access its object?

So this is the way to go. But If you want to read further that how and what should be conventions consider reading this

Community
  • 1
  • 1
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54