File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor 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 "Constructor" Watch "Constructor" New topic
Author

Constructor

Blanka Li
Greenhorn

Joined: Feb 18, 2002
Posts: 13
"Every class has constructor". Is the line right?
Does Object class has constructor?
Does abstract class has constructor?
If abstract class has constructor, when its subclass is initialized, is the abstract class' constructor called? We know abstract can't be initialized.
who can tell me?
with many thanks!
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219

"Every class has constructor". Is the line right?

Almost all of them as far as I know. If you are able to instantiate, they should have one, otherwise the JVM will provide a default no argument constructor
Does Object class has constructor?

YES. Refer API
Does abstract class has constructor?

YES Refer API. For example, Number class.
If abstract class has a constructor, when its subclass is initialized, is the abstract class' constructor called? We know abstract can't be initialized.

The basic design of abstract class is in such a way that it can not be instantiated but it can be extended. Extending is different from instantiation. By extending, you are inheriting the basic class features, but when you instantiate, you are actually creating an object of that kind.
Hope this helps. Correct me guys, if I am wrong.
[ June 22, 2002: Message edited by: Thiru Thangavelu ]

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Would you clarify on this?
The basic design of abstract class is in such a way that it can not be instantiated but it can be extended. Extending is different from instantiation. By extending, you are inheriting the basic class features, but when you instantiate, you are actually creating an object of that kind.

does this mean that when the default no arg constructor is called for an abstract class, JVM automatically calls the super's default no arg constructor, therefor what is instantiated is not the abstract class but the super class?
never mind
I just got it
[ June 22, 2002: Message edited by: Chung Huang ]

Let us be showered in the light of confusion!
Blanka Li
Greenhorn

Joined: Feb 18, 2002
Posts: 13
Pls look this example:
class A
{
}
class B extends A
{
public static void main(String[] args)
{
new B();
}
}
//-----------------------
if run: java B
the initialization steps as follows:
1 members(here none) in class A is initialized and assigned the default value.
2 the constructor of A is called.
// so A is initialized a object.
3 members(here none) in class B is initialized and assigned the default value.
4 the constructor of B is called.
// so B is initialized a object.
if A is an abstract class
the constructor of A is still called. but can't be initialized to be an object, why? I don't know how to make a class into an object. That is the principium to initialize a class to be an object.
who can help me?
with many thanks!
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219

class A {
}
class B extends A {
public static void main(String[] args) {
new B();
}
}

Ok, First the basics,
When you extend or instantiate a class, you have to call the superclass constructor. Note that there are exceptions like you can not call private construtors, default constructors can not be from other packages etc etc.. Of course, there should be a matching constructor in the superclass when you call specific constructor. If you are not calling any specific constructor, the JVM will call the default no argument constructor of superclass, which is done behind the scenes.
Now, coming to your point, when you extend the abstract class, you are required to implement the abstract methods of superclass. That is only the requirement when you extend abstract class. You are not creating any object of abstract class here. But when you instantiate, you are creating an object which can be used somany other purposes.
Hope this helps
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hello,
To retain the original format of your code,
please use [/CODE] to end your code and [CODE]
to start your code.
I think the idea here is that when you instantiate
a class, its superclass is not instantiated as well. The appropriate content of the superclass
is being put into the content of the subclass.
This is not an instantiation because at least
we cannot see a reference to the superclass. If
this is an instantiation, why do we bother ourselves to extend the superclass and not just
simply have a reference to the said superclass and instantiate it as a member of any classes ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
First of all, let's make this clear - when an class is instatiated, the constructor of every superclass is invoked. Therefore, if you have the following structure:

If you were to instatiate class C, class A's constructor would be invoked, followed by class B's constructor, followed by class C's constructor.
Even if one or more of the classes are abstract, their constructors must still be invoked. If they weren't, how would the member variables be initialized? Be sure to check out the JLS, §12.4 Initialization of Classes and Interfaces and §12.5 Creation of New Class Instances for all sorts of details about this procedure.
Also, note that not all classes have constructors. Anonymous classes are exceptions to this rule. A constructor, by definition, has the same name as the class it is contained in. As an anonymous class has no name, it can have no constructor. You can, however, use an instance initializer to handle anonymous class initialization.
I hope that helps,
Corey


SCJP Tipline, etc.
Blanka Li
Greenhorn

Joined: Feb 18, 2002
Posts: 13
I see, thank you all!
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
The Class class has no constructor. But you don't need to know this.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Paul Villangca:
The Class class has no constructor. But you don't need to know this.

Where did you hear that?
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
i was getting confused reading the posts, so i tried some experiments. (Ranchhands please correct me if i'm wrong!)

This will compile fine, but class A has a non-default constructor, so now java won't allow something like A a = new A();
now extend the class:

this will give the following error on compile:

this happens because java automatically tries to call the default constructor. Since no default constructor exists in class A, it fails. and apparently java does not call the A(int foo) constructor even though class B has a constructor with the same signature. the solution is to use the super keyword.

now java is happy because it has a constructor from class A to call to start off the initialization process.


use the [CODE] tags - it makes it much easier for people to help you.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Jon,you says:
" the solution is to use the super keyword."
But I think a better solution is just put a do nothing no-args constructor in all the classes you design.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Alan Chong:
But I think a better solution is just put a do nothing no-args constructor in all the classes you design.

NO!
Sometimes, having a no-args constructor just doesn't make sense for the class. You shouldn't design around the programming syntax. Rather, you should program to match your design.
Corey
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
i intentionally left out the no-args constructor to show the error - failure is a great way to learn! and sometimes it is not desirable to have a default constructor, so that the programmer is forced to give values to some data.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
The Class class has no constructor. But you don't need to know this.

Heh. I'm actually not sure if this is true or not. What I'm sure is that the Class class has no public constructor. From the API:
Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Paul Villangca:
the Class class has no public constructor

That's correct.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi, I'd like to ask a question:
The instanceof operator takes two arguments. What exactly is the type of the second argument? More to the point, is it possible to make it dynamic (as opposed to hardcoding it)?
I RTFMed the JLS but it wasn't helpful. I used getClass() for the second argument (to make it dynamic) but I got rapped by a compile time error.
Any insights from anybody?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Constructor