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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

Is this topic present in the exam (inner classes)?

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

JakeVR
Posts: 2
Joined: Thu Feb 28, 2013 2:02 am
Contact:

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

Post 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"

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Are you sure you are running MyTest? I just tried it. It runs fine and prints "In MySub.ml()".
If you like our products and services, please help us by posting your review here.

Guest

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

Post 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();
}

}

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

openmic62
Posts: 5
Joined: Wed May 01, 2013 5:46 am
Contact:

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

Post 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

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

@openmic62: The correct answer is indeed "It will not compile". So I am not sure I understand your doubt.
-Paul.
If you like our products and services, please help us by posting your review here.

openmic62
Posts: 5
Joined: Wed May 01, 2013 5:46 am
Contact:

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

Post 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

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 8811 times
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Note sure what you mean. Please post your complete issue without referring to posts that are too old.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post 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();
   }
}

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

what is the issue?
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest