About Question enthuware.ocajp.i.v7.2.1018 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1018 :
Is this topic present in the exam (inner classes)?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
Officially, no. But some candidates have reported getting a question on inner classes. So we have kept a few such questions.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Feb 28, 2013 2:02 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
I tried to execute the code in this excercise (also added "someInterger"), but I get RuntimeException: "Exception in thread "main" java.lang.NoSuchMethodError: main"
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"
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();
}
}
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"
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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.
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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();
}
}
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();
}
}
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed May 01, 2013 5:46 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
From what I see, none of the 3 answers offered are correct:
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
- 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.
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
@openmic62: The correct answer is indeed "It will not compile". So I am not sure I understand your doubt.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed May 01, 2013 5:46 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
Paul,
My doubt arises as follows:
Test one poses this as the question:
Mike Rocha
My doubt arises as follows:
Test one poses this as the question:
Then it offers 3 mutually exclusive answer choices of which I must pick one. Here are the ones presented to me.What will be the output when the above code is compiled and run?
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.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.
Mike Rocha
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
The first option is "It will not compile". Please see attached image. Please confirm if you are not seeing this.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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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?
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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();
}
}
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
what is the issue?
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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.
{super(a); } is not required because the compiler adds this line based on the call for anonymous class.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
Again, not sure what was your doubt in the first placevchhang wrote:I think I've figured it out.
That is correct.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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1018 :
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.
Who is online
Users browsing this forum: No registered users and 65 guests