• 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

 
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
I was excepting james here james as output
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output will be "here james" which is correct. This is beacuse call to super() results in the constructor of alpha class Object's constructor being called and not your custom made Object class. Also your objecttest is not extending your custom made Object class.

By creating a Object class doesn't mean that you are replacing the Object class in Java API with that of yours.
 
James Tharakan
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
but by the statement
new Object();
using the the custom class??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your result is:
here
james

The explanation of this result is as following:
Every class extends java.lang.Object. You created an Object class in a different package. This is why you don't see 'james' when you're initializing a 'objecttest' object.

The new Object() statement creates in instance of you own Object class, because the compiler probably checks first if an Object class exists in the same package as the Objecttest class. If this class does not exists in the current package, the Object class of package java.lang will be used.
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you call new objecttest()--- it prints "here" call to super() prints nothing.
and when you call new Object()--- your custom Object class constructor gets called and "james" is printed.

hence "here james"
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,

The super() method called in the constructor of the objecttest class calls the constructor of the default Object class of the java.lang package.

You need to extend the Object class in the objecttest class to print your expected output.

class objecttest extends Object

Thanks
Hemnath
 
James Tharakan
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
Doesn't a custom made class hide the Alpha class Object.
 
Jeroen Kema
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Tharakan wrote:Doesn't a custom made class hide the Alpha class Object.



No, because the custom made Object class also extends java.lang.Object
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CAn any one explain me what exactly is happening in th eprogram??
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice Q
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if you use fully-quallified-names, it will be clear:


(pseudo-code)
So you see, there is simply no inheritance-relationship between those two classes.
 
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
well the java compiler is smart enough. If you don't extend a class from any other class, it doesn't add extends Object, it adds extends java.lang.Object. So you code would look like this to the JVM



and thus the output would be here james...
 
James Tharakan
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

Ankit Garg wrote:well the java compiler is smart enough. If you don't extend a class from any other class, it doesn't add extends Object, it adds extends java.lang.Object.


Makes sense... thanks man..
 
This tiny ad is suggesting that maybe she should go play in traffic.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic