Java Local Nested Classes and Access to Super Methods

I have the following class hierarchy scenario; Class A has a method, and class B extends class A, where I want to call a method from a superclass from a locally nested class. I hope the skeletal structure illustrates the scenario more clearly. Does Java allow such calls?

class A{ public Integer getCount(){...} public Integer otherMethod(){....} } class B extends A{ public Integer getCount(){ Callable<Integer> call = new Callable<Integer>(){ @Override public Integer call() throws Exception { //Can I call the A.getCount() from here?? // I can access B.this.otherMethod() or B.this.getCount() // but how do I call A.this.super.getCount()?? return ??; } } ..... } public void otherMethod(){ } } 
+7
source share
3 answers

You can simply use B.super.getCount() to call A.getCount() in call() .

+21
source

You should use B.super.getCount()

+5
source

Something along the lines

 package com.mycompany.abc.def; import java.util.concurrent.Callable; class A{ public Integer getCount() throws Exception { return 4; } public Integer otherMethod() { return 3; } } class B extends A{ public Integer getCount() throws Exception { Callable<Integer> call = new Callable<Integer>(){ @Override public Integer call() throws Exception { //Can I call the A.getCount() from here?? // I can access B.this.otherMethod() or B.this.getCount() // but how do I call A.this.super.getCount()?? return B.super.getCount(); } }; return call.call(); } public Integer otherMethod() { return 4; } } 

perhaps?

+4
source

All Articles