• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor question

 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I came to know that in a inherited structure like a inherits b, b inherits c etc with each having a constructor when i create a object first constructor of Object is executed . Does it mean that memory for any object is actually defined in the Object(Object is the base class for all existing java objects) constuctor method ?
Also please help me with
what will happen if I create an A object and A doesn't have a constructor
defined whereas B has a constructor defined ?
What if in the above case B has a constructor with 2 arguments and I have to call it in A's constructor ?

Thanks
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you don't try write such class where A has default constructor and B has constructor with parameters? You will see what is the result.

But there will be compilation error.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that every new type has some 'Object-ness' to it, yes. In recent versions of Java many elements of the Object class are made static to account for that fact.

Every new type has a constructor, no matter what. You can write the access rights to a constructor so that no one outside the class can call it, but there still must be a constructor for every class. It makes no difference if a class provides its own or if the compiler silently inserts one to maintain the chaining process back to java.lang.Object, the basae supertype.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic