Weird class cast behavior

So I have this code:
        class SearcherAllocator extends SearcherBase.Pointer_Alloc{
            @Override
            public Pointer call(){
           
                class JSearcher extends Searcher{

                }

                return new JSearcher();

            }
        }
        Searcher.alloc(new SearcherAllocator().retainReference());

        Searcher searcher = (Searcher) Searcher.alloc().call();

May look a bit messy but essentially the class hierarchy of
Searcher
is
Searcher extends SearcherBase extends Pointer. However, even though Searcher extends Pointer, when the line
Searcher searcher = (Searcher) Searcher.alloc().call();

runs, it gives me this:
Exception in thread "main" java.lang.ClassCastException: class org.bytedeco.javacpp.Pointer cannot be cast to class com.niki.nativeplayer.Bridge.Searcher (org.bytedeco.javacpp.Pointer and com.niki.nativeplayer.Bridge.Searcher are in unnamed module of loader 'app')

there are two
Searcher.alloc()
functions, one is a setter and one is a getter. I think that part is self explanatory.
I tried to replicate this error with this:
    public static void main(String[] args) {
        class Pointer{


        }
        class SearcherBase extends Pointer{


        }
        class Searcher extends  SearcherBase{

        }

        Supplier<Pointer> allocator  =()->{
            class JSearcher extends Searcher{

            }
            return new JSearcher();
        };
        Searcher searcher= (Searcher) allocator.get();
}

But this code ,which should be pretty much equivalent, doesn't throw that exception.
Was this page helpful?