Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Interesting Notarajon's Mock # 30!

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class MyException extends Exception{}
2.
3. class superclass{
4.
5. protected int a;
6. protected int b;
7. void print(){
8. System.out.println("a1 = "+a+" b1 = "+b);
9. }
10.
11. }
12.
13. class Q30{
14.
15. superclass createinner() throws MyException{
16.
17. return new superclass() {
18. {
19. a = 2;
20. b = a*=2;
21. }
22.
23. void print(){
24. System.out.println("a = "+a+" b = "+b);
25. }
26.
27. };
28. }
29.
30. public static void main(String[] args) throws MyException{
31. new Q30().createinner().print();
32. }
33.
34. }
Would you be interested to expalin the lines: 15, 17 and 31.
Thanks,
A
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Albert, I will give it a shot. First thing that I did was to upgrade this code to the 'CattleDrive' styleguide conventions.
This changed the location of curly braces and capitalized 'Superclass' which is really just an identifier.
I also changed the name of Q30 to Q30xxxx and the name of method print() to printX(), just to avoid confusion between the original code and my modifications.
Next, I added some location 'println-s' to determine which printX method will run.
Then, I compiled the code, ran it, and discovered that the printX in Q30xxxx (actually within createinner which is within Q30xxxx) is the one that runs. Since I had re-arranged the margin positions of the curly braces, it was easy to see why this was the case, as well as why line 31.
'Superclass' is simply a class name and is used as the return type of createinner (this is why the 'return Superclass ... ' statement. A new Superclass instance is created and the instance variables get manipulated.
The createinner inner class defines a void method print, and that is why line 31 looks like it does.
Sorry that I did not spend more time on explanation, I am just on break for my final review before SJPC tomorrow. Work with it more, you will find a better explanation, then post it. Thanks for the challenge!

class MyException extends Exception{}

class Superclass
{
protected int a;
protected int b;
void printX()
{
System.out.println("In Superclass");
System.out.println("a1 = "+a+" b1 = "+b);
}
}

public class Q30xxxx
{
Superclass createinner() throws MyException
{
return new Superclass()
{
{
a = 2;
b = a*=2; // *= multiplies 'a' by 2
} // and stores result in a,
// a now stores 4 which is
// assigned as new value
// of b
// hence, the output of:
// a = 4 b = 4

void printX()
{
System.out.println("In Q30xxxx");
System.out.println("a = "+a+" b = "+b);
}
};
}

public static void main(String[] args) throws MyException
{
new Q30xxxx().createinner().printX(); // Output:
} // In Q30xxxx
} // a = 4 b = 4
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is testing you on anonymous class, overriding, and instance initializer concepts.
Line 15: superclass createinner() throws MyException
1) declares an instance method named createdinner in class Q30
2) the method's parameter list is empty ()
3) the method returns an object reference of type superclass
4) the method body may throw exception castable to MyException, if so the exception does not need to be in a try-catch block inside the method, it is propogated up to the caller
Line 17: return new superclass() {/* codes line 17 to 27*/ }
1) note line 17 ends in line 27, where the semi-colon is ";"
2) it returns an anonymous object reference that is declared and created between line 17 and 27,
3) the anonymous class extends superclass
4) the anonymous object reference is up-casted to superclass type because of the return type of the createdinner() method
Line 31: new Q30().createinner().print();
1) first created object of type Q30 - new Q30()
2) call the createinner() method on the newly created object reference which returns a anonymous object reference
3) call the print() method on the anonymous object reference, due to dynamic binding, the print method started in line 23 is called

 
Albert Gray
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tom.
Huiying thank you for taking your time. Some points still not clear to me. Should anonymous class not be defined inside a method and the semicolon should not be after ");"?
You saying "the anonymous object reference is up-casted to superclass type", I think I am not following this at all.. I mean how casting is being done here?
Thanks for your further comments.
Regds,
A
 
huiying li
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should anonymous class not be defined inside a method and the semicolon should not be after ");"?
Normally this is the case, however anonymous class can be defined and instantiated anywhere a reference can be used. Mughal page 243.
---
You saying "the anonymous object reference is up-casted to superclass type", I think I am not following this at all.. I mean how casting is being done here?
This is because of the return type of the method, the return reference is upcasted to this type. This is true for all return types, anonymous or not, even for primative types. Here is a program to illustrate this point.

Compile and run this program with line 2 (uncomment it) and without line 2. As far as the compiler is conerned, the method() returns a type A, at line 2, so assign it to a class B which is a sub class of class A is not allowed without explicit cast (as is line 3).
Hope this helps.

[This message has been edited by huiying li (edited March 12, 2001).]
    Bookmark Topic Watch Topic
  • New Topic