question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inheriting methods from Java in PythonJavaClass

See original GitHub issue

How do I inherit fields and methods from a Java parent class into a PythonJavaClass?

Consider the following Java base class:

package org.jnius;
public abstract class AbstractParent {
    int value=0;
    protected AbstractParent(){}
    public void doOne() { value = 1; }
    public abstract void doTwo();
}
class TestAbstract(with_metaclass(MetaJavaClass, PythonJavaClass)):
            __javaclass__ = 'org/jnius/AbstractParent'
            __javainterfaces__ = []
            
            @java_method('()Z')
            def doTwo(self):
                print("hello")

I can call doTwo(), but I cannot access doOne() from Python, nor value, i.e. neither of the following two statements work:

value = JavaField('()I;', static=False)
doOne = JavaMethod('()Z')

Moreover, we can’t mix Python classes that are children of PythonJavaClass and JavaClass.

It seems that PythonJavaClass (c.f. resolve_fields() in jnius_export_class.pxi) only examines self.__class__.__dict__ to find methods to add to the proxy class, while I could contend that, similar to autoclass(), it should walk the interfaces and superclasses.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tshirtmancommented, Apr 19, 2020

i think that was solved by your doc update PR, closing, let me know if you feel more is needed.

0reactions
xloemcommented, Aug 23, 2022

@tshirtman android now provides for loading dex classes at runtime, which can be used to subclass java classes at runtime. beeware is pursuing this. ref: https://github.com/beeware/rubicon-java/issues/25#issuecomment-1165640203

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inheritance - Learning the Java Language
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited...
Read more >
API — Pyjnius 1.0a1 documentation - Read the Docs
The idea is to subclass this JavaClass, add few JavaMethod , JavaStaticMethod , JavaField ... Decoration function to use with PythonJavaClass .
Read more >
Inheritance in Java Example - DigitalOcean
Inheritance in Java is the method to create a hierarchy between classes by inheriting from other classes. Java Inheritance is transitive ...
Read more >
Guide to Inheritance in Java - Baeldung
Simply put, in Java, a class can inherit another class and multiple interfaces, while an interface can inherit other interfaces.
Read more >
Java Inheritance (Subclass and Superclass) - W3Schools
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found