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

Object Class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai all,
As we know that java compliler will provide a default no-arg constructor,if we does not have any user defined constructor in our class.

How the object class constructor will be invoked and all the object class methods will be available to our class..??

Please claerify this doubt.

Thanks in advance,
Josweth Reddy
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)If we do not provide any constructor then default constructor will be added implicitly by the compiler to that class.
2)Now what I mean by default constructor is :
Your class: class A {}

This is what compiler generates for you:

class A extends java.lang.Object {
A() {
super(); // call to Object class constructor.
}
}

3)You can check this using javap JVM command.
class A {
}
>javac A.java
>javap A
class A extends java.lang.Object{
A();
}

4)If you extend some class other than java.lang.Object

Your code:class A {}
class B extends A {}

And this is what compiler generates for you:

class A extends java.lang.Object {
super(); // call constructor of Object class.Impilcit
}

class B extends A { // Now it doesn't extend Object class

super(); // call to superclass A constructor.
}

Let me know if thier is any mistake.
 
Josweth Reddy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
Thank you for the response.
But my doubt is ,How can we acces the Object class methods wothout creating an instance for the Object class.
For example:
we can apply equals() method without creating an instance for the Object Class.
How is it possible,to access the method of Object class without any instance???
Hope it is clear now.

Thanks in advance.
Josweth Reddy T.R
SCJP 1.4
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its because its[object class]is something that u get in the java.lang package which we all get by default and can use stuff like the System class or use any java keyword for that matter.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

That is a IS-A relationship.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic