Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

instantiating an interface

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why am i able to instantiate SharedPreferences.Editor interface and use its abstract methods?

code sample:


documentation: docs
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why am i able to call commit() if it is abstract?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but where am i implementing that(commit()) method? and also getSharedPreferences() is also abstract?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is an example that does not use anonymous classes.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this Share preference interface works like this(something like that):



 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, possibly. You would need to look at source code to know the details.
 
Saloon Keeper
Posts: 26070
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3919
542
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic