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

Directly extending Object

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
class ExtendsObject extends Object {
ExtendsObject () {
super("H"); //if this is not used, we get a compile error!!!
//compiler: Object(java.lang.String) in Object cannot
//be applied to ()
}
String name = "ExtendsObject class ";
void speak() {
System.out.println("I am in " + name );
}
public static void main (String [] args) {
ExtendsObject e = new ExtendsObject();
}
}
I would be grateful if someone could explain to me what is going on here and we cannot just compile without super("H"); .
Also I am pretty sure that in another case the compiler wouldn't complain if I used super() (or omitted it which is the same thing)
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must have some other 'Object' class (not java.lang.Object) in your classpath for that error. java.lang.Object does NOT require a String parameter in its constructor. Also, 'extends Object' is superfluous, as all objects implicitly extend Object unless they explicitly extend something else.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to double-check your code...
I get the error you describe only when the code contains super("H");
which makes sense since the base class (Object) doesn't have a constructor that takes a String parameter
Removing super("H"); or replacing it with super();will compile just fine.
 
karolos ignatiadis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just double checked and I get the same results. As a matter of fact I included the error I received from the compiler in the comments. To be on the safe side I will assume that there is no such think as a constructor in Object that takes a String as a parameter(according to the JLS and API Spec).
However I am still puzzled and as you said it might have something to do with my classpath.
Thank you for your help
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try running JWhich to determine where on your classpath the Object class is being found....
Take a look at this article:
JWhich Article
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try this:

Likely it doesn't print "java.lang.Object" but some other funky homemade class name.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic