aspose file tools*
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Can we modify constructors with abstract keyword?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what do you think ? constructor can not be overridden right?.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What happens when you try it?
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Rajiv : Did you find the answer to your question when you compiled the code ?

Jim ... ...


BEE MBA PMP SCJP-6
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Sep 29, 2008
Posts: 12617

@Jim: Er, that's not the OP.
john chris
Greenhorn

Joined: Aug 02, 2010
Posts: 8
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

Joined: Jul 11, 2010
Posts: 96


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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Jan 09, 2008
Posts: 525
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 ... ...
 
 
subject: constructors