Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Fri Oct 05, 2012 7:20 am
by ETS User
Is this topic present in the exam (inner classes)?

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Fri Oct 05, 2012 1:16 pm
by admin
Officially, no. But some candidates have reported getting a question on inner classes. So we have kept a few such questions.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Thu Feb 28, 2013 2:06 am
by JakeVR
I tried to execute the code in this excercise (also added "someInterger"), but I get RuntimeException: "Exception in thread "main" java.lang.NoSuchMethodError: main"

Code: Select all

class MySuper{
	public MySuper(int i){ }
}
abstract class MySub extends MySuper{
	public MySub(int i){ 
		super(i); 
	}
	public abstract void ml();
}
class MyTest{
	public static void main(String[] args){
		int a = 5;
		MySub ms = new MySub(a){
                        // super(a);|{super(a);}
			public void ml() { System.out.println("In MySub.ml()"); }
		};
		ms.ml();
	}
}
Please, help, what is now wrong with this program?
As it seems, this anonymous class doesn't have a constructor suitable for it's superclass, but how to create it? super(a); - throws "Illegal start of type", {super(a);} - throws "call to super must be first statement in constructor"

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Thu Feb 28, 2013 7:20 am
by admin
Are you sure you are running MyTest? I just tried it. It runs fine and prints "In MySub.ml()".

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Thu Mar 21, 2013 10:57 pm
by Guest
I'm also really confused by the explanation for this question. According to the Oracle documentation (http://docs.oracle.com/javase/tutorial/ ... asses.html), "you cannot declare constructors in an anonymous class." Since the constructor has the same name as the class and the anonymous inner class has no name, how can you name/declare the constructor?

So I don't think the anonymous inner class is truly a subclass of MySub. I was able to get the code working as shown below. The anonymous inner class seems to be using MySub's constructor as it's certainly not using a default constructor inserted by the compiler. I'm still confused. :) Any comments or explanations are greatly appreciated. Thanks!


class MySuper {an
MySuper(int i) {}
}

abstract class MySub extends MySuper {
public MySub(int i) { super(i); }
public abstract void m1();
}

public class MyTest {

public static void main(String[] args) {
int someInteger = 3;
MySub ms = new MySub(someInteger) {
public void m1() {System.out.println("In the sub"); }
};
ms.m1();
}

}

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Fri Mar 22, 2013 6:53 am
by admin
It is true that you can't declare constructor for an anonymous class explicitly. However, that doesn't mean it does not have any constructor. The compiler inserts a constructor automatically. To understand it completely, please read Section 15.9.5.1. Anonymous Constructors.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Wed May 01, 2013 5:53 am
by openmic62
From what I see, none of the 3 answers offered are correct:
  • It will throw an exception at run time.
  • It will print In MySub.m1()
  • It will compile and run without any exception but will not print anything.
Am I missing something?

I get the following compiler errors when I try to compile the code exactly as given in the Enthuware test question.

Q:\src\main\java>javac -d ..\..\..\target\classes MyTest.java
MyTest.java:13: error: constructor MySub in class MySub cannot be applied to given types;
MySub ms = new MySub(){
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
MyTest.java:13: error: constructor MySub in class MySub cannot be applied to given types;
MySub ms = new MySub(){
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
2 errors

Q:\src\main\java>javac -version
javac 1.7.0_07

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Wed May 01, 2013 6:48 am
by admin
@openmic62: The correct answer is indeed "It will not compile". So I am not sure I understand your doubt.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Wed May 01, 2013 8:55 am
by openmic62
Paul,

My doubt arises as follows:

Test one poses this as the question:
What will be the output when the above code is compiled and run?
Then it offers 3 mutually exclusive answer choices of which I must pick one. Here are the ones presented to me.
1. It will throw an exception at run time.
2. It will print In MySub.m1()
3. It will compile and run without any exception but will not print anything.
None of the answer choices offered are correct. So, I think maybe you need to revise the question or the answers? I hope this clears it up for you.

Mike Rocha

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Wed May 01, 2013 6:19 pm
by admin
openmic62 wrote: Then it offers 3 mutually exclusive answer choices of which I must pick one. Here are the ones presented to me.
1. It will throw an exception at run time.
2. It will print In MySub.m1()
3. It will compile and run without any exception but will not print anything.
The first option is "It will not compile". Please see attached image. Please confirm if you are not seeing this.

HTH,
Paul.
2.1018.gif
2.1018.gif (33.61 KiB) Viewed 12897 times

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 8:06 am
by vchhang
Excerpt from above post:

Are you sure you are running MyTest? I just tried it. It runs fine and prints "In MySub.ml()".

I tried the code and it will not compile either. Would you post the entire code to make that work?

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 8:53 am
by admin
Note sure what you mean. Please post your complete issue without referring to posts that are too old.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 9:06 am
by vchhang

Code: Select all

class MySuper{
   public MySuper(int i){ }
}
abstract class MySub extends MySuper{
   public MySub(int i){
      super(i);
   }
   public abstract void ml();
}
class MyTest{
   public static void main(String[] args){
      final int a = 5;
      MySub ms = new MySub(a){
         {super(a);} // will not compile.
         public void ml() { System.out.println("In MySub.ml()"); }
      };
      ms.ml();
   }
}

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 9:24 am
by admin
what is the issue?

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 9:50 am
by vchhang
I think I've figured it out. Please let me know if I am correct.

{super(a); } is not required because the compiler adds this line based on the call for anonymous class.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 10:05 am
by admin
vchhang wrote:I think I've figured it out.
Again, not sure what was your doubt in the first place :)
Please let me know if I am correct.

{super(a); } is not required because the compiler adds this line based on the call for anonymous class.
That is correct.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1018 :

Posted: Tue Jun 03, 2014 10:08 am
by vchhang
I wasn't sure about how the call to an anonymous class with an argument compiles and runs fine when we cannot define constructors for anonymous class. I didn't realize the compiler always creates a constructor for it based on the call as well as insert a call to the super class using the same argument.