• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class instantiation

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The anonymous class is actually a sub class of the abstract class.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i execute the above program i am getting comile time error could you explain it...
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ahmed, now every thing is clear...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15628
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    srinivas sridaragaddi
    Ranch Hand
    Posts: 225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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........
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic