• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sun sample exam - Item 6

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the Sun sample questions today at
WGS-PREX-J000: ePractice Sample Questions On item 6 the code goes like this:

Question: When inserted on line 10, which line will override the getType method and allow compilation to succeed?
Answer choices:
A o public Text getType(String a, int b, char c) {
B o public Text getType(int b, String a, char c) {
C o public String getType(String a, int b, char c) {
D o public String getType(int b, String a, char c) {
Sun's Answer:
Option C is correct because the method name, argument list, and return type are the same as the getType method defined in the IntType class. Overriding methods must be declared with the same
name, argument list, and return type as the overridden method.
Option A is incorrect because the return type is not the same as the getType method in the IntType class. Option B is incorrect because both the return type and argument list are different
from the getType method in the IntType class. Option D is incorrect because the argument list is different from the getType method in the IntType class.
My comment is this: Option C would have been correct if it had the static modifier for getType() method because it is being called from the static method main().
Am I missing something here? This test question is from Sun and I'm doomed if I get a question of similar context in the SCJP exam.

Also, is this the main Sun mock exams (10 questions)?
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Baud:
[B]I tried the Sun sample questions today at
WGS-PREX-J000: ePractice Sample Questions On item 6 the code goes like this:

Question: When inserted on line 10, which line will override the getType method and allow compilation to succeed?
Answer choices:
A o public Text getType(String a, int b, char c) {
B o public Text getType(int b, String a, char c) {
C o public String getType(String a, int b, char c) {
D o public String getType(int b, String a, char c) {
Sun's Answer:
Option C is correct because the method name, argument list, and return type are the same as the getType method defined in the IntType class. Overriding methods must be declared with the same
name, argument list, and return type as the overridden method.
Option A is incorrect because the return type is not the same as the getType method in the IntType class. Option B is incorrect because both the return type and argument list are different
from the getType method in the IntType class. Option D is incorrect because the argument list is different from the getType method in the IntType class.
My comment is this: Option C would have been correct if it had the static modifier for getType() method because it is being called from the static method main().
Am I missing something here? This test question is from Sun and I'm doomed if I get a question of similar context in the SCJP exam.

Also, is this the main Sun mock exams (10 questions)?
[/B]


Hi James,
You cannot use [b]static[\b] access modifier to override non-static access modifier. Here are the rules:
" It is illegal for a subclass to hide an instance method of its superclass withits own static method of the same signature. This will result in a compiler error." (JAVA Pitfalls, pg #5, by Mike Daconta, published by Wiley & Sons)
" It is illegal for a subclass to override a static method of its superclass with its own instance method of the same signature. This will also result in a compiler error." (JAVA Pitfalls, pg #5, by Mike Daconta, published by Wiley & Sons)
Regarding your comment about using 'static' modifer for getType() because it is being called by static method main(0 to achieve polymorphism, I don't quite understand what you try to convey. Sorry, I am not able to follow your reasoning to intelligently discuss. Let's hope somebody can.
Regards,
Lam
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops, sorry 'bout the slip. What I meant was, option C would have been correct if "both" overriding and overridden getType() methods were static. Or, another option would be to call getType() in main() through a OverType object reference, ie new OverType().getType(...). My point is, the program will not compile as it is, given option C.
Lam, try to compile the original code w/ option C and you'll see what I mean.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Baud:
Ooops, sorry 'bout the slip. What I meant was, option C would have been correct if "both" overriding and overridden getType() methods were static. Or, another option would be to call getType() in main() through a OverType object reference, ie [b]new OverType().getType(...). My point is, the program will not compile as it is, given option C.
Lam, try to compile the original code w/ option C and you'll see what I mean.
[/B]


Hi James,
I got your point now. Yes, even when you choose option C, you would still get compiler error since getType() is called without an object initialization (i.e the call from main() within subclass. So compiler treates it as class method (a.k.a. static context)). So I guess that the code needs to be changed as follows:
class IntType {
public String getType(String a, int b, char c) {
String holdit1 = new String();
holdit1 = a;
return holdit1;
}
}
class OverType extends IntType {
public String getType(String a, int b, char c) {
String holdit2 = new String();
holdit2 = holdit2.concat("This is ").concat(a);
return holdit2;
}
}
public class test {
public static void main(String[] args) {
String x = new String("x");
int y = 1;
char z = 'b';
OverType m = new OverType();
System.out.println(m.getType(x, y, z));
}
}
Well both wrongs don't make a right, do they?
But I hope my code would show another way out without forcing both getType()'s to be static. That would defeat the polymorphism spirit. Let's me be off on a tangent for a minute to share with you my two cents worth.
When one tries to override a superclass static method with a subclass static method (i.e both have the same name and signatures), one certainly invites trouble if s/he thinks polymorphism could be achieved or the superclass method could be overriden. Far from the truth, the superclass or parent class' static method cannot be overriden at all even compiler does not give a warning. Please try the following example:
class Parent {
static String greeting() {
return ("Goodnight");
}
String name() {
return (" James");
}
}
class Child extends Parent {
static String greeting() {
return ("Hello");
}
String name() {
return (" Lam");
}
}
public class test {
public static void main(String[] args) {
// create child object but declare to be Parent (polymorphic)
Parent P = new Child();
System.out.println)(P.greeting() + P.name() + "!");
}
}
Guess what - the answer is: Goodnight Lam!
So you can see, P.name() invokes Child's non-static nethod (i.e. we got polymorphism effect). But P.greeting() does not call the Child's greeting() but the Parent's because of static declaration for both greeting() methods! In short, there is no rule saying that one cannot declare the methods in both superclass and subclass to have the same name and same signuatures... However, in order to qualify which method to call, you have to attach class name with such invocation and that forces you to give up polymorphism.
Of course one can argue that in real life polymorphism should be performed on instances or objects. It is nonesense to apply it on class (static declaration) which is just an abstract. So who care?
Well, I hope that helps somewhat...
Regards,
Lam

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
Static methods can not be overridden.
Therefore using your logic of having the superclass method be static would not have made choice c correct!
Regards,
Manfred.
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, I'm with you 100% that static methods cannot be overridden, thx - sorry for the inappropriate use of the term and the concept. The only satisfactory correction availabe therefore is to call getType() via an object reference.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
reply
    Bookmark Topic Watch Topic
  • New Topic