sachink
sachink

Reputation: 395

Java Local Nested Classes and accessing super methods

I have following class hierarchy scenario; Class A has a method and Class B extends Class A, where I want to call a method from super class from a locally nested class. I hope a skeletal structure picture the scenario more clearly Does Java permit 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(){
  }
}

Upvotes: 7

Views: 3726

Answers (3)

Jonas Bystr&#246;m
Jonas Bystr&#246;m

Reputation: 26139

Something along the lines of

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?

Upvotes: 4

adarshr
adarshr

Reputation: 62583

You've to use B.super.getCount()

Upvotes: 5

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

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

Upvotes: 21

Related Questions