Can we define a parameter that allows any type that implements two or more interfaces?

Can we define a parameter that allows any type that implements two or more interfaces?

Yes, we can accomplish this by using generic types.

Suppose for example, that we have two interfaces A and B, and two classes, C that implements A and D that implements both A and B, as shown below.

interface A {
    void foo();
}
interface B {
    void bar();
}
class C implements A {
    public void foo() {
        System.out.println("foo");
    }
}
class D implements A, B {
    public void foo() {
        System.out.println("foo");
    }

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

Now we can define methods like test1() and test2() that restrict the types of values that are passed into the methods using generic types.

For test1(), the type of the value passed is into test1() is defined by T.  T is a generic type which represents a placeholder for a set of types.  If we used T alone, T would allow any type, which would be no different than declaring the parameter of type Object.  We have however constrained the type T by bounding the type.  The bounding is declared using <T extends A> before the return type of the method.  This bounds the argument to the method to being of any type that implements the interface A.  Note that even though A is an interface, we use the extends keyword rather than implements.  Since both classes C and D implement A, we can pass instances of C and D to test1().

When we define test2() we bound the parameter to allow any type that implements both A and B using <T extends A & B>.  Again, we use extends even though A and B are interfaces, and we use & to separate the interface names.  Since only class D implements both A and B, we can only pass instances of D to test2().

class Driver {
    public static void main(String[] args) {
        C c = new C();
        D d = new D();
 
        test1(c);
        test1(d);
        test2(c);    // will not compile
        test2(d);  
    }

    static <T extends A> void test1(T obj) {
        obj.foo();
    }

    static <T extends A & B> void test2(T obj) {
        obj.foo();
        obj.bar();
    }
}

Can an inner class call methods in the super class of the outer class?

Yes!  In the example below the class A is used as a superclass to the outer class. In it is a method named print.

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

The outer and inner class are defined below.

class Outer extends A {
    class Inner {
        void print() {
            Outer.this.print();
        }
    }
}

If we

 

 

 

 

 

 

Do variables in a while-loop get their own stack frame?

My initial answer was no, which is correct, but my reasoning was not.
Correct Answer:  They are stored in the stack frame created for the function in which they are declared and remain there until the stack frame is deallocated [Source].
So how does the JVM know if a variable is out of scope?  It doesn’t.
Scope is a compile time concept. When the JVM runs a Java program it assumes that the compiler has ensured that all references to variables are in scope.
Consider the following code:
void foo() {
    int y = 0;
    for (int i = 0; i < 5; i++) {
         y++;
    }
    System.out.println("i: " + i);    // i is out-of-scope
    System.out.println("y: " + y);
}
When we try to compile the above code we get the following compile error:
Driver.java:11: error: cannot find symbol
    System.out.println("i: " + i);
The variable i is out of scope in the first print statement because it was defined inside the for-loop and therefore can only be used inside the for-loop.  We are forced to remove the first print statement and recompile.
void foo() {
    int y = 0;
    for (int i = 0; i < 5; i++) {
        y++;
    }
    System.out.println("y: " + y);
}
So the compiler forces us to remove any statements that include variables that are out of scope.
Now when foo is executed, a stack frame is allocated on the stack for foo.  The variables y and i are pushed onto the stack in foo’s stack frame.  We know that all references to i are safe (any out-of-scope reference has been removed), so there is no harm done if the variable i remains in the stack frame even after the for-loop.  When foo is finished executing the entire stack frame is removed from the stack.
From what I’ve found the JVM specification does not require out of scope variables to be removed from the stack frame once they are out of scope since no harm can be done.