Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Inheritance syntax

 
Rob Keefer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code gives the errors listed below it. I have found some work arounds, but am wondering why this doesn't work. Can someone explain?
- Rob
----- Begin Code -----

--- Begin Error Messages ---
FooBar.java:7: cannot resolve symbol
symbol : constructor Foo (java.lang.String)
location: class Foo
Foo foo = new Foo( new String("Hello, world"));
^
FooBar.java:41: call to super must be first statement in constructor
super();
^
FooBar.java:46: call to super must be first statement in constructor
super( bs );
^
3 errors
(edited by Cindy to format code)
[ February 24, 2002: Message edited by: Cindy Glass ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you didnt name your constructors right.
constructors CANT return anything. not even void.
constrcutors are special function that return nothing and are called in the creation of objects.
modify all your constrcutors and remove the void keyword from them.
ps.
u dont need to extend from Object. all classes extend from Object by default. no need to write it.
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You put a return type (void) in the signatures of what you intended to be the constructors. A constructor does not have a return type. The return type (void) makes it an ordinary method, albeit one with a badly chosen name. And super() is legal only as the first statement of a constructor, never in an ordinary method. Since you didn't specify a valid constructor, the only constructor you have is the default no-arg constructor that Java creates when you don't specify a constructor.
 
Rob Keefer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys...
The joys of going from C++ to Java
- Rob
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually Rob, in c++ its exactly the same.
constructos dont return void in c++ also. they return nothing. the same as in java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic