Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer vs. Super class

Does super has higher priority than outer class?

Consider we have three classes:

  1. ClassA
  2. ClassB
  3. Anonymous class in ClassB that extends ClassA

ClassA.java:

public class ClassA {
    protected String var = "A Var";

    public void foo() {
        System.out.println("A foo()");
    }
}

ClassB.java:

public class ClassB {
    private String var = "B Var";

    public void test() {

        new ClassA() {
            public void test() {
                foo();
                System.out.println(var);
            }
        }.test();
    }

    public void foo() {
        System.out.println("B foo()");
    }
}

When I call new ClassB().test(), I get the following output (which is pretty much expected):

A foo()
A Var

Question: Is it defined somewhere that inner class takes (methods and members) first from the super class and then from the outer class or is it JVM compiler implementation dependent? I have looked over the JLS(§15.12.3) but couldn't find any reference for that, maybe it is pointed out there but I misunderstood some of the terms?

like image 527
MByD Avatar asked May 03 '11 09:05

MByD


People also ask

What is the difference between class and super class?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

What is an outer class?

Nested Classes In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.

What is the super class of class?

A superclass is the class from which many subclasses can be created. The subclasses inherit the characteristics of a superclass. The superclass is also known as the parent class or base class. In the above example, Vehicle is the Superclass and its subclasses are Car, Truck and Motorcycle.

Is every class a super class?

Which of these class is superclass of every class in Java? Explanation: Object class is superclass of every class in Java.


2 Answers

See 6.3.1 Shadowing Declarations:

A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.

Which may be interpreted as "the declaration of foo (inherited from ClassA) shadows the declaration of any other methods named foo that are in an enclosing scope (ClassB) at the point where foo occurs, throughout the scope of foo."

Also relevant - section 15.12.1:

15.12.1 Compile-Time Step 1: Determine Class or Interface to Search

The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:

  • If the form is MethodName, then there are three subcases:
    • If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier. If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.
    • If it is a qualified name of the form TypeName.Identifier, then [...]
    • In all other cases, the qualified name has the form FieldName.Identifier; then [...]
like image 165
aioobe Avatar answered Oct 07 '22 12:10

aioobe


I think you are always going to get "A var".

This is because your test() method implementation is being defined on an anonymous subclass of A. I don't think you can access the B.var instance variable within your test() method unless you explicitly refer to the outer class using ClassB.this.var.

like image 42
Captain Spandroid Avatar answered Oct 07 '22 12:10

Captain Spandroid