This discussion is archived
6 Replies Latest reply: Oct 10, 2012 7:07 AM by TPD-Opitz-Consulting-com RSS

A doubt on Inner classes

716130 Newbie
Currently Being Moderated
Hi,

The below program throws a compilation error saying "The method fn() in the type Outer.Inner is not applicable for the arguments (String)". I am expecting it to call the private String fn(String in) method on Outer class. This might be silly, but I am not able to find the reason why?

public class Outer {

private String fn(String in){
return in+"outer";
}

class Inner{
private String fn(){
return fn("");
}
}
}

Can anyone please shed some light on it?

Thanks in advance,
Jose John
  • 1. Re: A doubt on Inner classes
    EJP Guru
    Currently Being Moderated
    The reason is that the inner fn(), not being an override (i.e. not taking the same arguments), is instead an overload (different arguments), and overloads hide what they overload in outer scopes.
  • 2. Re: A doubt on Inner classes
    716130 Newbie
    Currently Being Moderated
    Thank you very much for your answer.

    It makes sense if an override hides what they override from outer scopes. But when an overload hides what they overload from outer scopes, basically we are loosing a method or making a completely different method no accessible right?

    Thanks,
    Jose John
  • 3. Re: A doubt on Inner classes
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    EJP wrote:
    The reason is that the inner fn(), not being an override (i.e. not taking the same arguments), is instead an overload (different arguments), and overloads hide what they overload in outer scopes.
    This is wrong. No inheritance is involved here.

    My Eclipse gave me a quickfix for this:
        class Inner {
            private String fn() {
                return Outer.this.fn("");
            }
        }
    The point may be that you cannot access a non static method without an object.

    bye
    TPD
  • 4. Re: A doubt on Inner classes
    gimbal2 Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    EJP wrote:
    The reason is that the inner fn(), not being an override (i.e. not taking the same arguments), is instead an overload (different arguments), and overloads hide what they overload in outer scopes.
    This is wrong. No inheritance is involved here.
    I fail to see where EJP actually mentions inheritance.
    The point may be that you cannot access a non static method without an object.
    YOU are wrong. The Eclipse quickfix is simply the standard way of referring to a member of an outer class even when the property/method is 'overlapped' by one of the inner class.
  • 5. Re: A doubt on Inner classes
    EJP Guru
    Currently Being Moderated
    Inheritance is implied by what I said about overloading and overriding. l accept the correction. What is happening is that Inner.fn() being in the nearest scope, it is hiding the fn() declared in the outer scope. Yes that does mean 'losing' a method. It is how the language is defined. Same in C++, for the record.
  • 6. Re: A doubt on Inner classes
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    gimbal2 wrote:
    TPD Opitz-Consulting com wrote:
    The point may be that you cannot access a non static method without an object.
    YOU are wrong.
    I was just guessing and I think I communicated this by the words "may be"...
    The Eclipse quickfix is simply the standard way of referring to a member of an outer class even when the property/method is 'overlapped' by one of the inner class.
    Thank you for this explanation.

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points