• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method override question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to override a method int[] myMethod() in superclass, which of
the following is(are) legal in subclass (select 2 correct answers):
a)int[] myMethod(){...}
b)int myMethod(){...}
c)int myMethod[](){...}
d)int myMethod()[]{...}
e)[]int myMethod(){...}
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion the correct choice is "a" and "c".
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers are a) and d).
We already know you have to have the same signature to override a method. So we know that b) is absolutely wrong.
c) will give a compiler error, expecting parentheses after the method name.
e) will also give a compiler error, because it doesn't recognize []int as a return type.
By process of elimination, we know that a) and d) must be the answers.
The reason for a) being correct is very obvious since it's written exactly the same way as the method in the superclass.
d) is correct because essentially you are saying that the method returns an array and then the int says that it is an array of ints.
You could also say int []myMethod(){//return some int array};
Just like you can say for varibles:
int[] var1 = new int[5];
int []var2 = new int[5];
int var3[] = new int[5];
I understand it, but I don't know that I explained it very well. If anybody has a better explaination, I'd love to hear it.
April
[This message has been edited by April.Johnson (edited June 19, 2001).]
[This message has been edited by April.Johnson (edited June 19, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought a and c also, but I just ran it and it's actually a and d. I'm going to have to go look that up in the jls...

Dave
 
Haining Mu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is A, D.
There is explaination for D in JLS 8.4, it is allowed but old fashion.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry but [] int is the same as int [] so A
and E so i believe are both valid
Ricardo
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I looked this one up in Complete Java 2.. and it says the square brackets can go either before or after the array variable or method.
So A and D.

Terry
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did one example on this. It clearly says that the answer is a and d.
mymethod()[] is allowed in java to return arrays

Originally posted by Haining Mu:
to override a method int[] myMethod() in superclass, which of
the following is(are) legal in subclass (select 2 correct answers):
a)int[] myMethod(){...}
b)int myMethod(){...}
c)int myMethod[](){...}
d)int myMethod()[]{...}
e)[]int myMethod(){...}


 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
test and error codes:
<pre>
class TestSuper {
int[] methodA() { return new int [1]; }
int[] methodB() { return new int [1]; }
int[] methodC() { return new int [1]; }
int[] methodD() { return new int [1]; }
int[] methodE() { return new int [1]; }
}

class Test extends TestSuper{
public Test() {
}
int[] methodA() { return new int [1]; }
// int methodB() { int i = 0; return i; } //method methodB() in class Test cannot override ....
// int methodC[]() { return new int [1]; } //';' expected at line 25, column 16
int methodD()[] { return new int [1]; }
// []int methodE() { return new int [1]; } //illegal start of type at line 29, column 3
}
</pre>
reply
    Bookmark Topic Watch Topic
  • New Topic