This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract " Watch "Abstract " New topic


Nilesh Kadu

Joined: Jul 06, 2001
Posts: 3
Pls let me know, whether it is possible to have a constructor of an Abstract class. If yes, pls share .
Thanks in advance .
vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
As far as my knowledge goes, they can have constructors but you cannot create an instance, bcoz abstract classes cannot be instantiated.
Please correct me if i am wrong.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Nilesh,
The abstract class can have constructors.
It is most likely that you would subclass the abstract class to provide the abstract method implementation. Also, the abstract class may have some common attributes which you would like to initialize while creating an instance of the subclass.In that case, the abstract superclass constructor would initialize it, when you create a subclass object, as the first statement of the subclass constructor is implicitly super().
The following code illustrates this fact :

Hope this helps,
SCJP2,OCSD(JDeveloper),OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 23, 2001).]

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Great Sandeep,
U explain it, in a real nice way.
but will u pleaze explain me one thing what r u doing by
by this.i = i; at fourth row.
I am really new to all these stuff.
One thing that i am not understanding is this that u are creating an constructor of Class TesterB but not of Class TesterA Which is actually an abstract class.
Please correct me if i am wrong in the following steps -
First u create a class TesterA then u declare an private integer i as shown below -
abstract class TesterA { //create an abstract class TesterA
private int i; //Declare a private integer i
public void seti(int i){ //define a method seti Which return integer value
this.i = i; // ?
public int geti() { //Define a method geti which returns i
return i;
public abstract void method();//here u declare an abstract method
public TesterA() { // Creating an constructor of TesterA
seti(100); // here integer i getting a value 100
System.out.println("In abstract class");
class TesterB extends TesterA { //Here u r creating a subclass of testerA
public TesterB() {//Creating a constructor of TesterB
System.out.println("In concrete class");
public void method() {
System.out.println("abstract method implementation");
public class Tester //A Class Tester
public static void main(String[] args)
TesterB b = new TesterB();//Creating constructor of class TesterB
System.out.println("The value of i is " + b.geti());
} }
//Can u create a constructor of an abstract class withour subclassing it.
UR any help will be highly appreciated.
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Hello Nisheeth,
When Desai writes this.i = i he does so because the method
seti(int i) defines the parameter int i which shadows the class member int i. In order to access the class member as aposed to the local method parameter i from within method seti() you must use this.i or fully qualified class name.i.
The other question asking why Desai does not expressly call the constructor of the abstract super-class is that the sub-class makes an implicit call to its own superclass constructor -new.super() - in order to properly initialize its inherited variables. This implicit chaining of constructors will continue all the way up the inheritance hierarchy to the constructor of Object.
I hope I understood your questions correctly.
Kind regards,
james hoskins.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Nisheeth,

Originally posted by Nisheeth Kaushal:
Can u create a constructor of an abstract class withour subclassing it.

Abstract class is just like any other class.It can have constructors like any other class - whether you subclass it or not.If you donot provide a constructor, it is going to have a default constructor.
Hope this helps,
jQuery in Action, 2nd edition
subject: Abstract