answersLogoWhite

0


Best Answer

Sure. An overridden method can return anything it wants.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can an overridden method return void instead of an object?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


What does 'public static final int' mean?

public: It can be called (method) or accessed (field) from any class in any package.static: It is declared on the class rather than the object. If a method, you do not need an object to call it, it can be called directly on the class. If a field, there is only one variable for the class, not one per object.final: If a method, the method cannot be overridden. If a field, the value cannot be changed (a constant).int: If a method, the return type. If a field, the field type (it can only hold values of type 'int'). int is the primitive integer type.


How do you prevent method overriding in java?

Use the word "final" directly preceding your method declaration. The presence of final keyword directs java to ensure that, this particular method would not be overridden by any of its child classes.


What are the rules for overriding a method in Java?

Realistically, the only real rule is that you may NOT override a method which has been declared 'final' by a superclass. By inference, you of course cannot extended a class that has itself been declared 'final', so no method in a final class can be overridden.In addition to the above restriction on whether you are permitted to override a method, there are several restrictions which you must obey when creating an override method:The scope (visibility) of the method may not be more restrictive than the one being overridden. For example, a method which is declared 'public' cannot be overridden with one which is declared 'package' or 'private'The override method's declared exceptions must be a subset of the original (i.e. you can removed Exceptions to be handled, but never add new ones that aren't a subtype of already existing Exceptions).The return type must either stay the same, or be a subclass of the original method's return type.And, of course, the declaration of arguments (type and number) must not change; otherwise, you are writing an Overloaded method, not an Overridden method.


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.

Related questions

Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


How do you return an object in java?

With the command return, followed by an object variable. In the method header, you have to declare the return type as the class of the object.


What does 'public static final int' mean?

public: It can be called (method) or accessed (field) from any class in any package.static: It is declared on the class rather than the object. If a method, you do not need an object to call it, it can be called directly on the class. If a field, there is only one variable for the class, not one per object.final: If a method, the method cannot be overridden. If a field, the value cannot be changed (a constant).int: If a method, the return type. If a field, the field type (it can only hold values of type 'int'). int is the primitive integer type.


How do you prevent method overriding in java?

Use the word "final" directly preceding your method declaration. The presence of final keyword directs java to ensure that, this particular method would not be overridden by any of its child classes.


What are the rules for overriding a method in Java?

Realistically, the only real rule is that you may NOT override a method which has been declared 'final' by a superclass. By inference, you of course cannot extended a class that has itself been declared 'final', so no method in a final class can be overridden.In addition to the above restriction on whether you are permitted to override a method, there are several restrictions which you must obey when creating an override method:The scope (visibility) of the method may not be more restrictive than the one being overridden. For example, a method which is declared 'public' cannot be overridden with one which is declared 'package' or 'private'The override method's declared exceptions must be a subset of the original (i.e. you can removed Exceptions to be handled, but never add new ones that aren't a subtype of already existing Exceptions).The return type must either stay the same, or be a subclass of the original method's return type.And, of course, the declaration of arguments (type and number) must not change; otherwise, you are writing an Overloaded method, not an Overridden method.


How do you search for an object in a list and if matches return that object using java?

Using boolen Contains(Object ob) method of List Interface .we can find object through List....


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.


What is premain method in java?

A premain method is launch in java from jdk 1.5 for instrumentation. In a very simple world we can say that a premain method is used for get the size of the object resevered in heap area. It will return byte reserved by the object. This method is similar to the sizeOf() function of c/c++. In earlier version before 1.5 it was not possible to get the size of an object but after that you can get the bytes reserved by an object in heap with premain function.


Why method overloading is not possible by return type method?

A method can be used, and the return type discarded. For example, for a method that returns, say, an integer, instead of calling it like this: int x; x = MyMethod(1, 2); You can also call it like this: MyMethod(1, 2); That is, without using the return value. In such a case, if you were to have different methods with the same parameters but different return values, the compiler wouldn't know which version of the method to use.


How many values can a method return in Java?

A method in java can declare only one return value and type at a time. For ex: a single method cannot have a code that returns a string in some cases and an integer in other cases. Java compiler does not let you do that. You can only have one return type for every method in java.


Applet class is not inheriting AppletContext interface but in Applet Class getAppletContext method is returning AppletContext object how is it possible?

There's nothing in Java that says that a method in a class can only return object types that the class inherits from. Your Applet is running in some sort of environment (a context, if you will). The Applet.getAppletContext method returns an AppletContext object which describes this environment.


How can a method return an object?

With the return statement, followed by the object you want to return. For example, in Java:String myMethod1() {return "abcde";// Note: The above is shorthand notation; you can also use the new operator.}MyDate myMethod2() {MyDate x = MyDate(2012, 1, 1);return x;// Note: This assumes you have created the class MyDate,// With the corresponding constructor.}