• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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!
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"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 ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Blanka Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Blanka Li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thank you all!
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Class class has no constructor. But you don't need to know this.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Villangca:
The Class class has no constructor. But you don't need to know this.


Where did you hear that?
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Villangca:
the Class class has no public constructor


That's correct.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic