• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on overriding

 
Diaz Thomas
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a question from JavaCaps Mock exam.
39. Which of the following statements are true?
A) An inner class cannot be defined as private.
B) Static methods can be overridden by static methods only.
C) Static variables can be called using class name.
D) Non static variables can be called using class name.
The answer given is C.
But I think B is also right. A static method cannot be overridden by a non-static method. That implies B is right. Can someone clarify this for me.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can't be overridden, period. They can be hidden by static methods. This topic is being discussed in at least one other active thread in this forum.
 
Diaz Thomas
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Thomas. I remember reading that static methods cannot be overridden. But I was testing the following piece of code. Is this overriding of a static method? Forgive me if I am making a blunder.
class ClassA {
static void print() {
System.out.println("Class A");
}
}
public class ClassB extends ClassA {
public static void main(String args[]) {
ClassB b = new ClassB();
b.print();
}
static void print() {
System.out.println("Class B");
}
}
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. That is hiding.
Overriding implies polymorphism and static methods don't participate in polymorphism because they are inlined at compile time.
 
Diaz Thomas
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But I am bit
 
Jim Crawford
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read through this as well... Maybe you'll understand better.
http://www.coderanch.com/t/239320/java-programmer-SCJP/certification/Overriding-hiding-methods
 
Diaz Thomas
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helped a lot Jim. I feel more confident now.
Thanks
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Diaz, here's another example:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic