• Post Reply Bookmark Topic Watch Topic
  • New Topic

instantiating an interface  RSS feed

 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am i able to instantiate SharedPreferences.Editor interface and use its abstract methods?

code sample:


documentation: docs
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Losty wrote:Why am i able to instantiate SharedPreferences.Editor interface and use its abstract methods?


There is nothing in your code that is instantiating an interface... it is merely calling a method that is returning an instance that IS-A that interface.

Henry
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why am i able to call commit() if it is abstract?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of having an abstract method is to have in implemented in some concrete class.
If this weren't allowed there would be no point in having an abstract method at all!
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but where am i implementing that(commit()) method? and also getSharedPreferences() is also abstract?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not implementing this method.

It is already implemented by whoever wrote the class that represents a type returned by sharedPreferences.edit().

An example:
In this example there is Animal interface. Animal can make a sound.
The method makeSound() is abstract.

Now, you are given RandomAnimalFactory. It returns some random animal.
For a moment assume this is in a different file and you can't see the source of this class.

All you can now is that it returns an instance of Animal.
Also, imagine there is an approproate javadoc.

The code you write is the Temp class.
Assume you need a random animal, so you can create an instance of RandomAnimalFactory and call getRandomAnimal() on it.
This returns you an Animal instance.

What class it is? You don't know. At least not until you checks the source of RandomAnimalFactory or call animal.getClass().
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that means the commit() is called from some other class( I will call it XYZ) which implements this(SharedPreferences.Editor) interface ?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this method is called by you whenever you need to call it.

It is implemented by some other class. That means the code that specifies what exactly commit() should do is written in some other class.
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:. . .. . .
There is a name for that construct: an anonymous class. You can read about it in the Java™ Tutorials.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paweł Baczyński

As far as I know you cannot do this:

If Animal is an interface ?


I think this may be a solution, something like that:


then you create it something like this:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Losty wrote:As far as I know you cannot do this:

If Animal is an interface ?

Animal is an interface.

The construct I used is called anonymous class and is perfectly legal.
Campbell already mentioned it.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Animal is an interface.

The construct I used is called anonymous class and is perfectly legal.
Campbell already mentioned it.


Perhaps using anonymous classes as an example is too complex? Why not something simple like an ArrayList instance and the List interface?

Henry
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the diference between



They both return an animal reference but the first will not compile??
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is an example that does not use anonymous classes.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Losty wrote:what is the diference between



They both return an animal reference but the first will not compile??


Sorry for confusing you with anonymous classes. Forget about it for a moment.

First line tries to create an object of type Animal. This is illegal.
Second line returns a reference of type Animal. It is legal.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are try to say that this works, I know that this works:



but I don't understand this, here we are creating an Animal instance with "new Animal" - which is an inteface:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to understand the concept of anonymous classes you can read this tutorial.

Also, note that this is illegal:
But this is legal:
In the second example an instance of a class with no name (anonymous class) that happens to implement Animal interface is created.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this Share preference interface works like this(something like that):



 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, possibly. You would need to look at source code to know the details.
 
Tim Holloway
Bartender
Posts: 18715
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot instantiate an interface. An Interface is a contract, not an object type.

You can instantiate an object of a concrete class that implements that interface - either directly - or via inheritance. Abstract classes may implement interfaces, but by definition cannot be themselves instantiated.
 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:... You would need to look at source code to know the details.

Here are some links to version 4.4.4 of the source if you want to dig-in:

getSharedPreferences(String, int)
SharedPreferences interface
SharedPreferences implementation
SharedPreferences.Editor implementation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!