| Author |
Object Class
|
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
When a class i written in java
like Then since we havent written any constructor for it . the compiler sees that and places a constructor like this and also writes super(); in it .
Am i right till here ?
1) when super () ; key word is there its function is to call the superclass's dummy constructor(no argument constructor ) .We know that Object class is the mother of all classes (super class) .
2) I didnt find any constructor in Object class like that while referring the api . so since the Object class doesnt contain any constructor .it should be a compiler error i suppose !
But why is the compiler ignoring it .
Is MY way of thinking right..correct me ranchers
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Am i right till here ?
No compiler will add constructor having the same access modifier that you have given to you class.
so in this case:
2) I didnt find any constructor in Object class like that while referring the api . so since the Object class doesnt contain any constructor .it should be a compiler error i suppose !
But why is the compiler ignoring it .
Compiler sees no constructor in Object class, so it adds default constructor there.
|
SCJP 6
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
did you check api clearly?
default constructor is available for Object class in api..
so when super(); is invoked it executes super class constructor which is default constructor in Object class...
|
SCJP5 and SCWCD1.5
Think Twice Act Wise...
|
 |
Sachin Adat
Ranch Hand
Joined: Sep 03, 2007
Posts: 213
|
|
srikanth mycherla wrote:2) I didnt find any constructor in Object class like that while referring the api .
Where did you get it in the api? Can you send me the link please......
|
SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
Actually in source code of Object class, there is no constructor.
|
 |
Pawel Nowacki
Ranch Hand
Joined: Nov 14, 2008
Posts: 67
|
|
There is public Object() constructor, i think.
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
ya.Punit is right .There is no default constructor in Object class.but punit You are saying that the compiler will add a constructor
will it again add in the same way as it added in the class.then where will the call go when super is called .
There is no super classs of object class ? right?
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
link
he mentioned as Object() in constructor summary so that we can assume that default constructor is in Object class..
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
I think compiler must have different mechanism for this, as Object class has no superclass.
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
if the compiler can add default constructor then it can have public modifier because Object class is havng public modifier..
but here in api he doesnt mention any modifier like public so that we can treat that it is having default modifer
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
sorry punit .there is a default constructor in object class.
punit if my class is public (obviously since ill have main method in that class) how come the line
public super() (added by the compiler ) can invoke default constructor of object class ?
confused!!!
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ganeshkumar cheekati wrote:if the compiler can add default constructor then it can have public modifier because Object class is havng public modifier..
but here in api he doesnt mention any modifier like public so that we can treat that it is having default modifer
See Public modifier is mentioned in API, click on Object() link, it will show you.
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
srikanth mycherla wrote:sorry punit .there is a default constructor in object class.
punit if my class is public (obviously since ill have main method in that class) how come the line
public super() (added by the compiler ) can invoke default constructor of object class ?
confused!!!
Why public super();, it is just super(), it is just a construct designed by java designers to call superclass constructors, if you super() than it will call superclass' no-arg constructor, if super(arg) than it will call superclass constructor that will take the argument.
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
|
i agree with you .but will the compiler add public constructor if the classs is default ? In the book it did like this .Is that right?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
srikanth mycherla wrote:i agree with you .but will the compiler add public constructor if the classs is default ? In the book it did like this .Is that right?
No compiler will add constructor with default access if class access modifier is default.
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
|
if compiler is adding the default construtor, then its access modifier is same as the class access modifier
|
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
|
SO finally there is an default constructor is available in Object class with public access modifier
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ganeshkumar cheekati wrote:SO finally there is an default constructor is available in Object class with public access modifier
Ya it is added by compiler.
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
Punit Singh wrote:
Ya it is added by compiler.
Mr.punit
will it added by compiler at compile time?
or
Is it available in Object class?
If there is no one available then compiler can add def constructor but api saying that there is an def constructor in Object class.
|
 |
Sachin Adat
Ranch Hand
Joined: Sep 03, 2007
Posts: 213
|
|
Punit Singh wrote:Ya it is added by compiler.
I guess not, it is already there in the Object class.
Just check the api, it says it has a default constructor.
Constructor Summary
Object()
Now, since it is in object class, it will not call super() because Object is the parent class of all classes which does not inherit from any other class.
How is this question related to SCJP.......... :roll:
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ganeshkumar cheekati wrote:
Punit Singh wrote:
Ya it is added by compiler.
Mr.punit
will it added by compiler at compile time?
or
Is it available in Object class?
If there is no one available then compiler can add def constructor but api saying that there is an def constructor in Object class.
Why do not you open source code of Object class and see there ?
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
Punit Singh wrote:
Why do not you open source code of Object class and see there ?
can you send me the link?
i just saying based on api...
|
 |
Pawel Nowacki
Ranch Hand
Joined: Nov 14, 2008
Posts: 67
|
|
i would say i've told you, but i won't
Besides, Punit, what does it mean:
Ya it is added by compiler.
I mean its there "always", is'nt it? Not need for compiler to add it or maybe you meant subclass's constructor and default constructor call to Objects added there?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ganeshkumar cheekati wrote:
Punit Singh wrote:
Why do not you open source code of Object class and see there ?
can you send me the link?
i just saying based on api...
It is in JAVA_HOME/src.zip .
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
yes punit you are right .
compiler can add default constructor with public modifier..
but they have mentioned as
public Object()(Constructor summary) in api so that i thought it is available source code.
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ganeshkumar cheekati wrote:yes punit you are right .
compiler can add default constructor with public modifier..
but they have mentioned as
public Object()(Constructor summary) in api so that i thought it is available source code.
They have mentioned for use-purpose, when you will use Object class, then default constructor will be added by default by the compiler and If you see detail of constructor in API, it is blank, they have not provided any detail. And you can see detail of String() constructor they have provided at least detail of 2 lines.
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
ok punit...
let us stop our conversation..
every one can get clear if they can watch our posts..
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
ya i suppose
in object class there is a constructor whose access specifier is default .
but if we dont write any constructor in our class then the compiler will add one public constructor for us
as public classname()
{
super();
}
the specifier is public since we may extend the class across packages
default is not added by the compiler even our class is default .it adds public constructor in order to work across packages
even if the user prefixes public before his class name at any stage there wont be a problem
so it adds public default constructor irrespective of class scope
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
srikanth mycherla wrote:default is not added by the compiler even our class is default .it adds public constructor in order to work across packages
You are wrong. The compiler adds a constructor to a class with no constructors which has the same accessibility as the class. So if you have a public class, then the constructor added by the compiler will be public. But if your class is package visible, then the constructor added by the compiler is also package visible. Just think about it. If your class is package visible, then what will be the benefit if the compiler adds a public constructor. The class will still be inaccessible outside the package.
And yes the object class has no constructor in it's source code. So the compiler creates a constructor in it. This is a special constructor which doesn't call super as it's first statement...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
but ankit in api i can see the constructor
as object()
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Object
public Object()
I am seeing this in api, try to scroll down or click on the Object().
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
ya but ankit is saying there is no constructor in object class ! but i can see constructor in api..confused!
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
srikanth mycherla wrote:ya but ankit is saying there is no constructor in object class ! but i can see constructor in api..confused!
I told you na, compiler is adding public Object(){}, in source code there is no constructor, as I had said in earlier replies. Read all replies once again, you will get everything.
|
 |
Sachin Adat
Ranch Hand
Joined: Sep 03, 2007
Posts: 213
|
|
Punit Singh wrote:
srikanth mycherla wrote:ya but ankit is saying there is no constructor in object class ! but i can see constructor in api..confused!
I told you na, compiler is adding public Object(){}, in source code there is no constructor, as I had said in earlier replies. Read all replies once again, you will get everything.
Ankit said it is not in the source code, please read it completely.....
Does anyone think that post is too hot, with not much value.
Yes, the Object class has a constructor. The api says it has one. The source code doesn't have it(I haven't checked it though).
Either it has one, or it has one added by the compiler. But what is the problem if it is either?
There are so many problems in java, and here we are fighting if Object has a constructor or not.
Aren't there more important problems to solve?
Can any one tell me if this topic is helpful for preparing in SCJP?
Does this question really belong to the SCJP forum.........
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
You are right Sachin, but after this question some ranchers will be able to see java source code also beside java api, and also some ranchers still do not know how to see apis completely, they will be more careful in future.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Whatever dudes! It doesn't matter if the source code has a constructor or not :lol: . The main deal is that it doesn't call super. And as sachin said, this type of thing will not be asked in SCJP. So peace all
|
 |
srikanth mycherla
Greenhorn
Joined: Oct 31, 2008
Posts: 26
|
|
YA .What Ankit said is absolutely correct .....
regards --Ankit , punit and sachin
lets discuss the burning topics here after
|
 |
 |
|
|
subject: Object Class
|
|
|