• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

annonyme class

 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Freinds


please can someone explain for me why we don't have compile error in this line

c1 c = new c2().m1();

 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adil, I believe you have forgotten to format your code with proper indentations again?

Like your previous post, https://coderanch.com/t/620034/java-programmer-SCJP/certification/Threads-Synchronization, this post also has no indentations. In this post also, you have not quoted your source. Quoting the source of the code often helps with copyrights.

I also have one another question. Is your previous post - https://coderanch.com/t/620034/java-programmer-SCJP/certification/Threads-Synchronization resolved to your liking? You said 'please someone can explain for me the output i'm lost' but you did not specify which part of the output you could not understand.

Likewise in your current post, you ask - 'please can someone explain for me why we don't have compile error in this line'. So how about you first tell us why you think it shouldn't compile.

I believe you might want to consider doing the following.
1. Edit your code with proper formatting and indentations so it is readable to the rest of us.
2. Quote your source ( the book from where you've copied this piece of code).
3. Explain why you think that particular line should not compile.
4. Go back to your previous post and quote your sources, if applicable.
5. Explain what you were looking for in your previous post and if it is addressed to your liking so someone can mark it as closed.

Thanks,
Chan.
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Ranch ,
i m sorry if i can't explain good, and yes i m agree with you thanks .
i saw that cod in this website

http://www.skillsign.com

, you can test your skills for pass the SCJP 6 , here the test have 7 test

http://www.skillsign.com/individual/user/uplan/viewPlan

.

for this question


the out put is

anonyme class m1


my problem is that i coudn't understand why we call m1() two tiems and i have just one output
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adil,

Formatting the code means changing your code to look something like below.



Doesn't it look better now? Isn't it amazing to see how much of a difference these few spaces can make to the readability of code. It makes it easy for us to see where a block starts and where it ends.

Another thing is, we don't code class names like that. The accepted standard is that class names start with a capital letter and when we have class names made up of more than one word, we capitalize the first letter of each word.
Example -
MyClass
Inverter
C1 is better than c1 though a class name should also be meaningful and c1 or C1 or MyClass are far from it.
I know you've copied this code from somewhere, but this is just so you don't code such class names when you are writing your classes.

my problem is that i coudn't understand why we call m1() two tiems and i have just one output


When you say new c2(), you are creating a new c2 object. Through this object, you are invoking c2's m1 method. Now my question to you is what happens when you invoke c2's m1 method.

Hint - Anonymous classes. Question - Do you know what is an anonymous class? Do you know what is an inner class?
If not, I strongly recommend you to go through http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html first. Read about Nested classes, Inner class examples, local classes, and anonymous classes.
Let us take this question after you've done that. What say?


Chan
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chan ,
for the question

- Do you know what is an anonymous class? Do you know what is an inner class?


yes i knew what is an inner class and thanks for the link you give me .

and for the part i didn't understand ,


i hop my analyse was correcte !
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right. c2's m1 method returns the object of an on the spot created subclass of c1 and this subclass ( an anonymous class ) overrides c1's m1.

So when you invoke m1 method in the second line on this returned object, it is the subclass' overridden m1 which is invoked and it prints 'anonyme class m1'.

Chan.
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Chan
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic