• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

abstract class instantiation

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ahmed, now every thing is clear...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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........
     
    Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic