Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Majji exam1 Qno2

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look for the following code
class Test
{
static void show()
{
System.out.println("Show method in Test class");
}
}
public class Q2 extends Test
{
static void show()
{
System.out.println("Show method in Q2 class");
}
public static void main(String[] args)
{
Test t = new Test();
t.show();
Q2 q = new Q2();
q.show();

t = q;// --------------------line 1
t.show();
q = t; //--------------------line 2
q.show();
}
}
prints "Show method in Test class"
"Show method in Q2 class"
"Show method in Test class"
"Show method in Q2 class"
what effect does line 1 and line 2 make on the result???
 
Michael Burke
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case the two assignments have no effect because the two show() methods are declared static.
 
Andy Boyibolomy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you should change the line 2 to q = (Q2) t; so that it runs smoothly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic