• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Object Class

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

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.

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

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......
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually in source code of Object class, there is no constructor.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is public Object() constructor, i think.

http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
link

he mentioned as Object() in constructor summary so that we can assume that default constructor is in Object class..
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think compiler must have different mechanism for this, as Object class has no superclass.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if compiler is adding the default construtor, then its access modifier is same as the class access modifier
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SO finally there is an default constructor is available in Object class with public access modifier
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

Punit Singh wrote:Ya it is added by compiler.


I guess not, it is already there in the Object class.

Pawel Nowacki wrote:
There is public Object() constructor, i think.
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html


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

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

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


Pawel Nowacki wrote:

There is public Object() constructor, i think.

http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html



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

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

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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok punit...
let us stop our conversation..
every one can get clear if they can watch our posts..
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but ankit in api i can see the constructor
as object()
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Object

public Object()



I am seeing this in api, try to scroll down or click on the Object().
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya but ankit is saying there is no constructor in object class ! but i can see constructor in api..confused!

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YA .What Ankit said is absolutely correct .....

regards --Ankit , punit and sachin

lets discuss the burning topics here after
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic