Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we modify constructors with abstract keyword?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you think ? constructor can not be overridden right?.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try it?
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code




It gives following errors in class "One"


1: Illegal modifier for the method One.One()
2: This method requires a body instead of a semicolon

at line 4

and in class "Test1" following error is displayed

"Return type for the method is missing" at line 10
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Chelsea wrote:and in class "Test1" following error is displayed

"Return type for the method is missing" at line 10

That's because the constructor name must match the class name, and Test1 != One.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv : Did you find the answer to your question when you compiled the code ?

Jim ... ...
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i asked my friendly compiler....... he said


Q.java:10: invalid method declaration; return type required
Q.java:3: modifier abstract not allowed here...... now it should be clear! !
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, just wanted to be sure. You reported the error messages
but did not say that you understood them. Have a good week.

Jim ... ...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jim: Er, that's not the OP.
 
john chris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,I am still learning english

at line 4:

constructors need body

at line 10:

the JVM regard as it is a normal method,not aconstructor, so it need return type
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


at line 4:

constructors need body

at line 10:

the JVM regard as it is a normal method,not aconstructor, so it need return type



When you put all together....Compiler says only normal method can be associated with abstract with return type.

 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every constructor must have a name identical to the class name and no return type. If you state a return type by mistake, the compiler will take that as a method. Probably a design fault in the language, but it is too late to change now.

Every method must have a return type or void.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The language does not restrict method names to exclude the class name.
If there's a return type, then it's a method, as shown in this confusing but
valid code.Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic