• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite instance creation

 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from javacertificate.com
The answer is 2,4,5
I think 5 is illegal because it cause infinite loop of creating instance. Any comment?

Consider the following class definitions, which of the following can be placed legally at line 7 without causing any compile time errors? [Check all correct answers]
1. public class Animal {
2. public Animal() {
3. super();
4. }
5. protected Animal (int legs){
6. this();
7. // Insert Code Here
8. }
9. }
10. public class Bird extends Animal {
11. public Bird() {
12. super();
13. }
14. public Bird(int legs) {
15. super(legs);
16. }
17. }

1 super();

2 Animal elephant = new Animal();

3 this(legs);

4 double d = Math.sin((double)legs);

5 Animal p = new Bird(4);
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, this will cause an infinite loop, <b>but</B> the code still compiles so it is legal to place the line: Animal p = new Bird(4);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic