Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SCJP practice question
10-21-2008, 10:08 PM
Post: #1
SCJP practice question
I have the following code:

public interface A{
public void doSomething(String string);
}
public class AImpl implements A{
public void doSomething(String msg){}
}
public class B{
public A doit(){
//some code
}
public String execute()
{
//more code
}
}
public class C extends B{
public AImpl doit(){
//more code
}
public Object execute()
{
//more code
}
}

Why does class C not compile here?
Find all posts by this user
Quote this message in a reply
10-21-2008, 10:24 PM (This post was last modified: 10-21-2008 10:25 PM by albinjoseph.)
Post: #2
RE: SCJP practice question
class C extends class B and overrides the execute method of class B. But the class B's execute method has String as the return type where class C's execute method has Object as the return type and that is the reason why class C is not compiling.

The Java Language specification mandates that if we are overriding a method, the return type and the exception that we are throwing must be same as the super class method.

From JLS 8.4.6.3
Code:
If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs
if they have different return types or if one has a return type and the other is void

http://java.sun.com/docs/books/jls/secon...tml#227965
Visit this user's website Find all posts by this user
Quote this message in a reply
10-21-2008, 10:30 PM
Post: #3
RE: SCJP practice question
thanks albin Smile
Find all posts by this user
Quote this message in a reply
10-21-2008, 10:31 PM
Post: #4
RE: SCJP practice question
will it work if class C has a method

public Object execute( with some args)
{
}

this would actually be overloading..so would it compile in this case?
Find all posts by this user
Quote this message in a reply
10-21-2008, 10:41 PM
Post: #5
RE: SCJP practice question
It will work only if you make C as abstract class. Else you need to have a method with name execute which returns a string object. You can have your execute method that takes some parameter also without any issues.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-21-2008, 10:46 PM
Post: #6
RE: SCJP practice question
But class B is not abstract. So if C extends B is it necessary that C should override all methods in B?
Find all posts by this user
Quote this message in a reply
10-22-2008, 01:45 AM
Post: #7
RE: SCJP practice question
priyaguptan Wrote:But class B is not abstract. So if C extends B is it necessary that C should override all methods in B?

Thats why I said if you are not overriding the execute method you need to make class C as abstract.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SCJP question 2 priyaguptan 1 525 10-22-2008 02:12 AM
Last Post: albinjoseph

Forum Jump: