Forums Register Login

constructors

+Pie Number of slices to send: Send
Can we modify constructors with abstract keyword?
+Pie Number of slices to send: Send
what do you think ? constructor can not be overridden right?.
+Pie Number of slices to send: Send
What happens when you try it?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Rajiv : Did you find the answer to your question when you compiled the code ?

Jim ... ...
+Pie Number of slices to send: Send
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! !
+Pie Number of slices to send: Send
Okay, just wanted to be sure. You reported the error messages
but did not say that you understood them. Have a good week.

Jim ... ...
+Pie Number of slices to send: Send
@Jim: Er, that's not the OP.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 



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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ... ...
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2947 times.
Similar Threads
another question on constructors
Constructors are not inhertied - True
Why I can't call the parent class constructor?
OOP 4: Lookup
Can constructor throw exception?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:45:51.