Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class and Initialisation block

 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

An abstract class can have an initialisation block.

Please explain the reason behind this as I am not clear. Initialisation blocks are executed when an instance of a class is created. We can not have an instance of an abstract class. Then why are abstract classes allowed to have initialisation blocks.

Below mention example (Source:http://www.examlab.tk/) compiles fine. I was thinking that the answer would be "Compilation fails".



Please explain.
 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can not have an instance of an abstract class. Then why are abstract classes allowed to have initialisation blocks.


Of course, you *can* have an instance of the class. You just can't instantiate it directly -- you have to instantiate a concrete subclass. A concrete subclass IS-A instance of the abstract class. (The initialization block is executed as part of the subclass' instantiation)

Henry
[ November 12, 2008: Message edited by: Henry Wong ]
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I got it now.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like Interfaces are not allowed to have static initialisation blocks. We can create their instance also through their subclasses. Then for interface, why it is not allowed to have an initialisation block.

I tried to compile the following program. It is giving me errors.

if I put line1 in comments it is compiling.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't interface instance variables inherently public static final? So you are trying to reinitialize a final variable?

Maybe wrong, just trying to figure out as well.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...Thanks Brian.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to define the String as: String s;

Then try to initialize it in the static block the same way you already have the code. I "think" you can initialize a final in an initialization block. Of course I may just be spouting garbage.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrm... guess I'm wrong. Just tried it and still has compiler issues.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian interface cannot have static or non-static intializers...
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the output of above code? is it "Demojava" ???
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no.the output is DemoC#
 
Aleem Ireland
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am struggling to understand what super.s means here. I was under the impression super means the superclass of YClass, which is XClass.

When XClass is loaded , the value is C++ and then it becomes C Sharp as its a static initializer. Does the String Java come into play at all ? I thought s holds JAVA due to super.s reference.

I have run this in eclipse also and still confused.

Cheers
 
swapnil kataria
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ouput is DEMOC#,

the reason i think when super.s will be called which will refer to s in super class, but value of s would have been changed then,
bcz of initialize blocks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic