Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract Class and Initialisation block 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 and Initialisation block" Watch "Abstract Class and Initialisation block" New topic
Author

Abstract Class and Initialisation block

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
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
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Thanks. I got it now.
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
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

Joined: Nov 07, 2008
Posts: 488
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.


SCJA
~Currently preparing for SCJP6
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Oh...Thanks Brian.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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

Joined: Nov 07, 2008
Posts: 488
Hrm... guess I'm wrong. Just tried it and still has compiler issues.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Brian interface cannot have static or non-static intializers...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
what is the output of above code? is it "Demojava" ???
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
no.the output is DemoC#


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Aleem Ireland
Greenhorn

Joined: Apr 08, 2011
Posts: 1
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

Joined: Feb 26, 2011
Posts: 64
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,
 
Don't get me started about those stupid light bulbs.
 
subject: Abstract Class and Initialisation block
 
Similar Threads
Help
init blocks problem
Exam Lab Question
Doubt in ExamLab
use of super -Examlab Question