how to access abstract class inside an interface - java

this is more of a java question rather than an android question. If we look at the code below, I need to access getEnrollmentUsername from my main class:
public interface IAirWatchSDKService extends android.os.IInterface {
public String getEnrollmentUsername(String publicKey);
public abstract static class Stub extends Binder implements IAirWatchSDKService {
private static class Proxy implements IAirWatchSDKService {
#Override
public IBinder asBinder() {
return null;
}
public String getEnrollmentUsername(String publicKey){
return "it worked";
}
}
}
}
but the problem is that it is wrapped inside of an abstract class. How can I get to it from my main class? And none of this code can change because it is part of a library, rather I need to write code from my main class only.

Proxy is declared as a private class. To access it from outside it needs public, protected or default(package private) depending on where you want to access it from.

Best practice is that if your main class needs to run this method, it should implement the interface. That, or the main class is aware of the interface with a method such as this: myIAirWatchSDKService.getEnrollmentUsername(s)

Related

How do i ensure implementation of a public static function with interface

Since Java 8, we are able to define static and default methods in interface. But I need to ensure a public static method say foo() to be implemented in all the classes that implements a particular interface say interface A. How do I do that , or is it at all possible ?
The interface A:
package com.practice.misc.interfacetest;
public interface A {
public static Object foo(); //Eclipse shows error : 'This method requires a body instead of a semicolon'
String normalFunc();
}
Class B :
package com.practice.misc.interfacetest;
public class B implements A{
#Override
public String normalFunc() {
return "B.normalFunc";
}
//I need to ensure that I have to define function foo() too
}
Class C :
package com.practice.misc.interfacetest;
public class C implements A{
#Override
public String normalFunc() {
return "C.normalFunc";
}
//I need to ensure that I have to define function foo() too
}
Edit 1:
Actual case :
I have one public static method getInstance() (returning Singleton instance of that class) in all the implementing classes, and I want to ensure all the future classes other developers write must have that static method implemented in their classes. I can simply use reflection to return that instance by calling the getInstance() method from a static method of the interface, but I wanted to make sure that everyone implements the getInstance() in all the implementing classes.
static methods from interface are not inherited (1). They are inherited in case of a class, but you can not override them (2); thus what you are trying to do is literally impossible.
If you want all classes to implement your method, why not simply make it abstract (and implicitly public) to begin with, so that everyone is forced to implement it.
Eugene already pointed out that static methods can not be overridden. I suggest that you extract the singleton behavior to a separate interface. For example:
public interface A {
String normalFunc();
}
public class B implements A {
#Override
public String normalFunc() {
return "B.normalFunc";
}
// TODO add getInstance for singleton
}
public interface Singleton {
// TODO extensive javadoc to describe expected singleton behavior
A getInstance();
}
public class BSingleton implements Singleton {
#Override
public A getInstance() {
return B.getInstance();
}
}
Finally you can use any object of type BSingleton to get the singleton object of B.

Find private inner class using reflection

In the context of android.accounts.AccountManager:
public class AccountManager {
private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
...
}
}
I would like to find the inner class by name using reflection, as I require a Class object to determine staticness of member methods. In the simple case
public class AccountManager {
public class Foo {
...
}
}
This can be done using Class.forName("android.accounts.AccountManager$Foo"). However, the private modifier eliminates this possibility. I tried searching through Class.forName("android.accounts.AccountManager").getDeclaredClasses(), but it returns an empty list. Its specification covers private member classes, so I am at a loss why it is not returned by the getter. I presume it has something to do with abstract.
EDIT: Experiments have shown getDeclaredClasses() to work with the following snippet, but not with the actual Android class.
public class Test {
private abstract class AmsTask extends LinkedList implements TestInterface {
...
}
private interface TestInterface{}
}
The suggestion that using AccountManager's class loader might help, did not turn out to be fruitful.
Class am = Class.forName("android.accounts.AccountManager");
Class.forName("android.accounts.AccountManager$AmsTask",false,am);
resulted in a ClassNotFoundException during the latter call.

How to design a common static method in all classes implementing an interface

I have an interface called Relation, implemented by a class BasicRelation, and extended by subclasses (e.g. ParentChild, Sibling, Spouse). While developing my code, I realized that I often need a method which takes a String representation of a relation to create it. For example:
public class ParentChild implements Relation extends BasicRelation {
// e.g. "Jack is Emily's father. Jill is her mother." will return the list
// <ParentChild(Jack, Emily), ParentChild(Jill, Emily)>
static List<ParentChild> fromSentence(String s) {
...
}
}
Now, since I find myself needing this method (fromSentence(String)) in every class, except perhaps in BasicRelation, I would like to move it up the hierarchy. The problem is that the internal details of the method is subclass-dependent, so I can't have it as a static method in the interface Relation or the superclass BasicRelation.
Unfortunately, in Java, it is also not possible to have a static abstract method.
Is there any way to ensure that every subclass of BasicRelation (or every class implementing Relation) implements fromSentence(String)? If no, should I be designing this in a completely different way? I guess this last question is more of a request for design-advice than a question.
Why does the static method need to be in the interface? What's stopping you from having a 'Utility' class and having the method in there?
public class RelationUtility {
public static BasicRelation relationFactory(String asString) {
....
}
}
As a static method, there is no reason other than access to private members, which can also be accomplished by by 'default' permissions on those members....
You can try making the BasicRelation class an abstract class and use an abstract fromSentence(..) method. This would require the ParentChild class to override and implement the fromSentence method because you can't create an object for ParentChild without implementing fromSentence()
public abstract class BasicRelation extends Relation(){
public abstract List<..> fromSentence(String s);
}
public class ParentChild implements Relation extends BasicRelation {
fromSentence(){
//parentChild class's implementation
}
}
If I understood right... you can try an approach like this
public class BasicRelation {
public abstract List<ParentChild> fromSentenceInSubclass(s);
public List<ParentChild> fromSentence(String s){
fromSentenceInSubclass(s);
}
}
And then you could have:
public class SubclassRelation extends BasicRelation {
public List<ParentChild> fromSentenceInSubclass(s){
// do subclass relation stuff
}
}
You will probably need to change the code a bit and add some Generics around to make it happen the way you want.
Sotirios Delimanolis Factory suggestion might also be an option.
You can have the abstract class BasicRelation include the static method which throws an Exception. That way you will be forced to override (shadow) the static method in the subclasses when you use it.
Something like:
public abstract class BasicRelation {
public static List<..> fromSentence(String s) {
throw new RuntimeException();
}
}

How can I instantiate an abstract class to remove a NullPointerException?

So the question title is a little weird but it is the only way I could think of the get the point across. I have a NullPointerException from doing this (The extra code is taken out):
public abstract class Generator extends SimpleApplication{
private static SimpleApplication sa;
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
I know that the private static SimpleApplication sa; is null but when I instantiate it I get this error instead of a NullPointerException :
SimpleApplication is abstract; cannot be instantiated
I am using the jMonkey Engine 3. If anyone knows how I could solve this problem, that would be great! Thanks!
If you take a close look at the JMonkey documentation, SimpleApplication is made to be extended by you to create your application.
You should create a new class that extends SimpleApplication and implement the missing methods. You then instantiate your custom class and pass it as a parameter.
A little like this :
public class myCustomSimpleApplication extends SimpleApplication {
// Implementing the missing methods
#Override
public void simpleInitApp() {
}
#Override
public void simpleUpdate(float tpf) {
}
// etc...
}
And then...
private static SimpleApplication sa = new myCustomSimpleApplication();
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
As others have commented, your base application class (I assume it is Generator) which extends SimpleApplication must not be abstract, i.e. it should implement, at least, the simpleInitApp method. See the JMonkey documentation for SimpleApplication or this basic example.
For completion, I will mention that if you really needed to create a dummy instance of an abstract class, you can do so "inline", without explicitly writing the full implementing class. You just need to write the implementation of the abstract methods enclosed by curly brackets after the constructor invocation:
public abstract class AbstractClass {
private static AbstractClass a = new AbstractClass() {
#Override
public void abstract_method() {/*implementation*/}
};
public abstract void abstract_method();
}
An anonymous class is created behind the scenes. But I definitely don't think this is what you need in this case.

how do i access a public method of a default class outside the package

I have a class with no modifier(default), which has a public method called mymeth. I know I could access the method when I am within the package. However I would like to access the method when I am outside the package. does anyone has an Idea on how it could be done. theoretically I think it should be possible since public method means access by the world. here is the example of my class and method:
class myclass{
public void mymeth(int i,int b){
.....
}
}
set myclass class to be public.
**FYI, Classes in Java start from upper Case letter
Directly you cannot. 'public' makes everything visible. But if you can't see the class, it's difficult to call anything. However,
You can extend the default class with a public class, eventually myMeth is exposed.
PubClass.java
package p1;
class DefClass{
public void myMeth(){
System.out.println("from myMeth!");
}
}
public class PubClass extends DefClass{
public PubClass(){
super();
}
}
MainClass.java
package p2;
class MainClass {
public static void main(String[] args) {
p1.PubClass pub = new p1.PubClass();
pub.myMeth();
}
}
output:
from myMeth!
A real practical use for this would be, overriding a public known method in that hidden class. You can implement a public method in a hidden class, so the world can call your public method (public implementation rather) without the class being exposed. For example the public method of the Object class is overridden here by DefClass:
PubClass.java
package p1;
class DefClass{
public String toString(){
return "DefClass here. Trying to explain a concept.";
}
}
public class PubClass extends DefClass{
public PubClass(){
super();
}
}
MainClass.java
package p2;
class MainClass {
public static void main(String[] args) {
p1.PubClass pub = new p1.PubClass();
System.out.println(pub.toString());
}
}
output:
DefClass here. Trying to explain a concept.
public interface SomeInterface{
public void mymeth();
}
class MyClass implements SomeInterface{
public void mymeth(){
}
}
//is in the same package as MyClass
public MyClassFactory{
public SomeInterface create(/*parameters*/){
//create instance from parameters
//For your case
MyClass instanceOfAnyClassThatImplementsSomeInterface = new MyClass(/*pass the parameters*/);
return instanceOfAnyClassThatImplementsSomeInterface;
}
}
One of the ways is already defined in answers but If you want to restrict the public access of the class then you can create an interface and access the method through it.
Set myclass as public then put it in the build path of the class you need to use myclass.
In your code, myclass has the default (package-level) access modifier. It should be declared using the public access modifier so that it is accessible outside its package. For details, read more about Controlling Access in Java.
As a side note, the Java standards require you to capitalize each word in the class name, so you should use MyClass. I recommend you the Java Conventions document.
Consider making another public class MyChild with the same package name as MyClass and expose the method from MyChild class
public class MyChild extends MyClass {
public void myTestMethod(){
super.myTestMethod
}
}
Now in your class where you want to use the method, simply use the instance of MyChild class
MyChild m = new MyChild();
m.myTestMethod();
Cheers :)

Categories

Resources