Instantiate Interface without Implementation Class
up vote
2
down vote
favorite
I have an Interface (call it Planet) whose implementation class is protected, and I can not change access modifier for the same. However, I need to call the methods of the implementation class inside of my main class. For example, in the implementation class, I have: public void orbit() distance++;
I have tried: private Planet planet = new PlanetImpl
, but since the PlanetImpl class is protected and in a different package, it can't find it, making the statement invalid. I have also tried leaving it null, like this Planet planet; planet.orbit();
but it throws a NullPointerException. Is it possible to call the methods without directly referencing the implementation class?
java
add a comment |
up vote
2
down vote
favorite
I have an Interface (call it Planet) whose implementation class is protected, and I can not change access modifier for the same. However, I need to call the methods of the implementation class inside of my main class. For example, in the implementation class, I have: public void orbit() distance++;
I have tried: private Planet planet = new PlanetImpl
, but since the PlanetImpl class is protected and in a different package, it can't find it, making the statement invalid. I have also tried leaving it null, like this Planet planet; planet.orbit();
but it throws a NullPointerException. Is it possible to call the methods without directly referencing the implementation class?
java
5
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
4
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
3
is theorbit
method part of the interface? How do you usually instantiate these objects?
– Mureinik
Nov 10 at 16:40
2
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have an Interface (call it Planet) whose implementation class is protected, and I can not change access modifier for the same. However, I need to call the methods of the implementation class inside of my main class. For example, in the implementation class, I have: public void orbit() distance++;
I have tried: private Planet planet = new PlanetImpl
, but since the PlanetImpl class is protected and in a different package, it can't find it, making the statement invalid. I have also tried leaving it null, like this Planet planet; planet.orbit();
but it throws a NullPointerException. Is it possible to call the methods without directly referencing the implementation class?
java
I have an Interface (call it Planet) whose implementation class is protected, and I can not change access modifier for the same. However, I need to call the methods of the implementation class inside of my main class. For example, in the implementation class, I have: public void orbit() distance++;
I have tried: private Planet planet = new PlanetImpl
, but since the PlanetImpl class is protected and in a different package, it can't find it, making the statement invalid. I have also tried leaving it null, like this Planet planet; planet.orbit();
but it throws a NullPointerException. Is it possible to call the methods without directly referencing the implementation class?
java
java
edited Nov 10 at 20:01
asked Nov 10 at 16:38
ChristianCoder
133
133
5
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
4
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
3
is theorbit
method part of the interface? How do you usually instantiate these objects?
– Mureinik
Nov 10 at 16:40
2
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10
add a comment |
5
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
4
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
3
is theorbit
method part of the interface? How do you usually instantiate these objects?
– Mureinik
Nov 10 at 16:40
2
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10
5
5
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
4
4
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
3
3
is the
orbit
method part of the interface? How do you usually instantiate these objects?– Mureinik
Nov 10 at 16:40
is the
orbit
method part of the interface? How do you usually instantiate these objects?– Mureinik
Nov 10 at 16:40
2
2
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10
add a comment |
6 Answers
6
active
oldest
votes
up vote
0
down vote
accepted
Assuming that private/protected impl class is inner class. To call the orbit() method which is a instance method(not static), You need to create a public static factory method in the outer class and call the orbit using the instance provided by this method.
basic code example
public class PlanetImplholder
public static Planet getInstance()
return new PlanetImpl();
private static class PlanetImpl implements Planet
@Override
public void orbit()
System.out.println("111");
call the method from anywhere like below.
Planet planet = PlanetImplholder.getInstance();
planet.orbit();
add a comment |
up vote
1
down vote
Using anonymous inner class we can do it. Anonymous classes can implement interfaces, and i think at that time only you'll have a chance to see a class implementing an interface without the "implements" keyword.
interface Planet
public void orbit();
class PlanetMain
private Planet p = new Planet ()
public void orbit()
System.out.println("interface Planet class executed");
;
add a comment |
up vote
0
down vote
In your case you have to create an object of that PlanetImpl
class somewhere to execute the method. The interface has only the definition of that method, not the implementation.
add a comment |
up vote
0
down vote
Is it possible to call the methods without directly referencing the implementation class?
NO, THIS IS NOT POSSIBLE. Interface and implementation class
are just declaration. To use it, you have to create class or interface instance with new
. But you cannot do it, because you want to use logic of PlanetImpl
, which is protected.
I think, that you do not want to implement your own class declaration and then create and use new instance of it. I think, that only one way is using reflection. Using it, you can create an instance of protected class PlanetImpl
and then use it in normal ways.
Class<Planet> cls = (Class<Planet>)Class.forName("<full PlanetImpl class name, inclusing package>");
Constructor<Planet> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Planet planet = constructor.newInstance();
planet.orbit();
add a comment |
up vote
0
down vote
I can't test it right now, but you can use reflection to access the class properties of an object.
Planet planetImpl = factory.getPlanet(); // getPlanet() returns PlanetImpl
Method orbit = planetImpl.getClass().getDeclaredMethod("orbit");
orbit.setAccessible(true);
orbit.invoke(planetImpl);
This may not work if a SecurityManager
prevents access to PlanetImpl.
add a comment |
up vote
0
down vote
So what I ended up doing was re-working my code so that I could call PlanetImpl from a different class. So now it's Universe universe = new Universe();
Thanks so much for all the help! And sorry for not trying this before asking the question.
Planet planet = universe.getPlanet();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241075%2finstantiate-interface-without-implementation-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Assuming that private/protected impl class is inner class. To call the orbit() method which is a instance method(not static), You need to create a public static factory method in the outer class and call the orbit using the instance provided by this method.
basic code example
public class PlanetImplholder
public static Planet getInstance()
return new PlanetImpl();
private static class PlanetImpl implements Planet
@Override
public void orbit()
System.out.println("111");
call the method from anywhere like below.
Planet planet = PlanetImplholder.getInstance();
planet.orbit();
add a comment |
up vote
0
down vote
accepted
Assuming that private/protected impl class is inner class. To call the orbit() method which is a instance method(not static), You need to create a public static factory method in the outer class and call the orbit using the instance provided by this method.
basic code example
public class PlanetImplholder
public static Planet getInstance()
return new PlanetImpl();
private static class PlanetImpl implements Planet
@Override
public void orbit()
System.out.println("111");
call the method from anywhere like below.
Planet planet = PlanetImplholder.getInstance();
planet.orbit();
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Assuming that private/protected impl class is inner class. To call the orbit() method which is a instance method(not static), You need to create a public static factory method in the outer class and call the orbit using the instance provided by this method.
basic code example
public class PlanetImplholder
public static Planet getInstance()
return new PlanetImpl();
private static class PlanetImpl implements Planet
@Override
public void orbit()
System.out.println("111");
call the method from anywhere like below.
Planet planet = PlanetImplholder.getInstance();
planet.orbit();
Assuming that private/protected impl class is inner class. To call the orbit() method which is a instance method(not static), You need to create a public static factory method in the outer class and call the orbit using the instance provided by this method.
basic code example
public class PlanetImplholder
public static Planet getInstance()
return new PlanetImpl();
private static class PlanetImpl implements Planet
@Override
public void orbit()
System.out.println("111");
call the method from anywhere like below.
Planet planet = PlanetImplholder.getInstance();
planet.orbit();
answered Nov 10 at 20:17
Sachin
1014
1014
add a comment |
add a comment |
up vote
1
down vote
Using anonymous inner class we can do it. Anonymous classes can implement interfaces, and i think at that time only you'll have a chance to see a class implementing an interface without the "implements" keyword.
interface Planet
public void orbit();
class PlanetMain
private Planet p = new Planet ()
public void orbit()
System.out.println("interface Planet class executed");
;
add a comment |
up vote
1
down vote
Using anonymous inner class we can do it. Anonymous classes can implement interfaces, and i think at that time only you'll have a chance to see a class implementing an interface without the "implements" keyword.
interface Planet
public void orbit();
class PlanetMain
private Planet p = new Planet ()
public void orbit()
System.out.println("interface Planet class executed");
;
add a comment |
up vote
1
down vote
up vote
1
down vote
Using anonymous inner class we can do it. Anonymous classes can implement interfaces, and i think at that time only you'll have a chance to see a class implementing an interface without the "implements" keyword.
interface Planet
public void orbit();
class PlanetMain
private Planet p = new Planet ()
public void orbit()
System.out.println("interface Planet class executed");
;
Using anonymous inner class we can do it. Anonymous classes can implement interfaces, and i think at that time only you'll have a chance to see a class implementing an interface without the "implements" keyword.
interface Planet
public void orbit();
class PlanetMain
private Planet p = new Planet ()
public void orbit()
System.out.println("interface Planet class executed");
;
edited Nov 10 at 17:19
answered Nov 10 at 17:01
Sivareddy
55
55
add a comment |
add a comment |
up vote
0
down vote
In your case you have to create an object of that PlanetImpl
class somewhere to execute the method. The interface has only the definition of that method, not the implementation.
add a comment |
up vote
0
down vote
In your case you have to create an object of that PlanetImpl
class somewhere to execute the method. The interface has only the definition of that method, not the implementation.
add a comment |
up vote
0
down vote
up vote
0
down vote
In your case you have to create an object of that PlanetImpl
class somewhere to execute the method. The interface has only the definition of that method, not the implementation.
In your case you have to create an object of that PlanetImpl
class somewhere to execute the method. The interface has only the definition of that method, not the implementation.
answered Nov 10 at 16:57
Sand
725111
725111
add a comment |
add a comment |
up vote
0
down vote
Is it possible to call the methods without directly referencing the implementation class?
NO, THIS IS NOT POSSIBLE. Interface and implementation class
are just declaration. To use it, you have to create class or interface instance with new
. But you cannot do it, because you want to use logic of PlanetImpl
, which is protected.
I think, that you do not want to implement your own class declaration and then create and use new instance of it. I think, that only one way is using reflection. Using it, you can create an instance of protected class PlanetImpl
and then use it in normal ways.
Class<Planet> cls = (Class<Planet>)Class.forName("<full PlanetImpl class name, inclusing package>");
Constructor<Planet> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Planet planet = constructor.newInstance();
planet.orbit();
add a comment |
up vote
0
down vote
Is it possible to call the methods without directly referencing the implementation class?
NO, THIS IS NOT POSSIBLE. Interface and implementation class
are just declaration. To use it, you have to create class or interface instance with new
. But you cannot do it, because you want to use logic of PlanetImpl
, which is protected.
I think, that you do not want to implement your own class declaration and then create and use new instance of it. I think, that only one way is using reflection. Using it, you can create an instance of protected class PlanetImpl
and then use it in normal ways.
Class<Planet> cls = (Class<Planet>)Class.forName("<full PlanetImpl class name, inclusing package>");
Constructor<Planet> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Planet planet = constructor.newInstance();
planet.orbit();
add a comment |
up vote
0
down vote
up vote
0
down vote
Is it possible to call the methods without directly referencing the implementation class?
NO, THIS IS NOT POSSIBLE. Interface and implementation class
are just declaration. To use it, you have to create class or interface instance with new
. But you cannot do it, because you want to use logic of PlanetImpl
, which is protected.
I think, that you do not want to implement your own class declaration and then create and use new instance of it. I think, that only one way is using reflection. Using it, you can create an instance of protected class PlanetImpl
and then use it in normal ways.
Class<Planet> cls = (Class<Planet>)Class.forName("<full PlanetImpl class name, inclusing package>");
Constructor<Planet> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Planet planet = constructor.newInstance();
planet.orbit();
Is it possible to call the methods without directly referencing the implementation class?
NO, THIS IS NOT POSSIBLE. Interface and implementation class
are just declaration. To use it, you have to create class or interface instance with new
. But you cannot do it, because you want to use logic of PlanetImpl
, which is protected.
I think, that you do not want to implement your own class declaration and then create and use new instance of it. I think, that only one way is using reflection. Using it, you can create an instance of protected class PlanetImpl
and then use it in normal ways.
Class<Planet> cls = (Class<Planet>)Class.forName("<full PlanetImpl class name, inclusing package>");
Constructor<Planet> constructor = cls.getDeclaredConstructor();
constructor.setAccessible(true);
Planet planet = constructor.newInstance();
planet.orbit();
answered Nov 10 at 20:39
oleg.cherednik
5,10821016
5,10821016
add a comment |
add a comment |
up vote
0
down vote
I can't test it right now, but you can use reflection to access the class properties of an object.
Planet planetImpl = factory.getPlanet(); // getPlanet() returns PlanetImpl
Method orbit = planetImpl.getClass().getDeclaredMethod("orbit");
orbit.setAccessible(true);
orbit.invoke(planetImpl);
This may not work if a SecurityManager
prevents access to PlanetImpl.
add a comment |
up vote
0
down vote
I can't test it right now, but you can use reflection to access the class properties of an object.
Planet planetImpl = factory.getPlanet(); // getPlanet() returns PlanetImpl
Method orbit = planetImpl.getClass().getDeclaredMethod("orbit");
orbit.setAccessible(true);
orbit.invoke(planetImpl);
This may not work if a SecurityManager
prevents access to PlanetImpl.
add a comment |
up vote
0
down vote
up vote
0
down vote
I can't test it right now, but you can use reflection to access the class properties of an object.
Planet planetImpl = factory.getPlanet(); // getPlanet() returns PlanetImpl
Method orbit = planetImpl.getClass().getDeclaredMethod("orbit");
orbit.setAccessible(true);
orbit.invoke(planetImpl);
This may not work if a SecurityManager
prevents access to PlanetImpl.
I can't test it right now, but you can use reflection to access the class properties of an object.
Planet planetImpl = factory.getPlanet(); // getPlanet() returns PlanetImpl
Method orbit = planetImpl.getClass().getDeclaredMethod("orbit");
orbit.setAccessible(true);
orbit.invoke(planetImpl);
This may not work if a SecurityManager
prevents access to PlanetImpl.
answered Nov 11 at 4:55
Ozan
3,66121735
3,66121735
add a comment |
add a comment |
up vote
0
down vote
So what I ended up doing was re-working my code so that I could call PlanetImpl from a different class. So now it's Universe universe = new Universe();
Thanks so much for all the help! And sorry for not trying this before asking the question.
Planet planet = universe.getPlanet();
add a comment |
up vote
0
down vote
So what I ended up doing was re-working my code so that I could call PlanetImpl from a different class. So now it's Universe universe = new Universe();
Thanks so much for all the help! And sorry for not trying this before asking the question.
Planet planet = universe.getPlanet();
add a comment |
up vote
0
down vote
up vote
0
down vote
So what I ended up doing was re-working my code so that I could call PlanetImpl from a different class. So now it's Universe universe = new Universe();
Thanks so much for all the help! And sorry for not trying this before asking the question.
Planet planet = universe.getPlanet();
So what I ended up doing was re-working my code so that I could call PlanetImpl from a different class. So now it's Universe universe = new Universe();
Thanks so much for all the help! And sorry for not trying this before asking the question.
Planet planet = universe.getPlanet();
answered Nov 15 at 13:47
ChristianCoder
133
133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241075%2finstantiate-interface-without-implementation-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Could you share the code as well?
– Nicholas K
Nov 10 at 16:39
4
why is the PlanetImpl class is private?
– nullpointer
Nov 10 at 16:39
3
is the
orbit
method part of the interface? How do you usually instantiate these objects?– Mureinik
Nov 10 at 16:40
2
Typically you would have a static method which returns an instance or you need to call the constructor. Otherwise you can create an instance.
– Peter Lawrey
Nov 10 at 16:57
Well, if your PlanetImpl class is private then you can't implement any interface in Java!
– Abhinav
Nov 10 at 17:10