It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class instantiation" Watch "abstract class instantiation" New topic
Author

abstract class instantiation

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225



What is the output:
prints:5
prints 6
prints 7
compile time error
runtime error


i thought it will be compile time error as "abstract class cannot be instantiated". But its working fine . dont know why its working fine ? is it because it a anonymous inner class object of a abstract class.


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
The anonymous class is actually a sub class of the abstract class.
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
but ahmed we are not extending it so is it implicitly being extended. and is it not instantiation? can you throw little more light on the topic.

And ahmed source of this request is javagalaxy which contains 500 java questions and all my questions are from that site. its a free registration under test your java skills section. I thought this might be helpfull to you or some one else...
[ October 04, 2007: Message edited by: srinivas sridaragaddi ]
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

we are not extending it so is it implicitly being extended. and is it not instantiation?

Yes, thats the syntax for creating anonymous classes that extend their classes, Or implement their interfaces:

Here we are not instantiating the interface, we create anonymous implementer of Runnable.
swati cha
Ranch Hand

Joined: Jul 04, 2006
Posts: 40
if i execute the above program i am getting comile time error could you explain it...
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
Thanks ahmed, now every thing is clear...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Originally posted by swati cha:
if i execute the above program i am getting comile time error could you explain it...

There are a number of problems in the code that Srinivas posted that prevent it from compiling. Some of them are:
  • abstract Class should have been class (Java is case-sensitive!)
  • A method "public void int Y()" - you can't specify "void" as well as "int" at the same time
  • The line "{int Y();}" in class B is wrong (I don't know what Srinivas wanted to do with this line)
  • Srinivas, if you copy and paste some code, please be accurate and precise, otherwise it will be hard to give you a correct answer.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    srinivas sridaragaddi
    Ranch Hand

    Joined: Jul 24, 2007
    Posts: 225
    hi jesper

    these are the questions from javagalaxy. where we cannot copy the code. so i was forced to re type the entire code. and thus lot of typo. i should have cross checked twice. sorry........
     
    jQuery in Action, 2nd edition
     
    subject: abstract class instantiation