• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

assignment

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Majji -Q2
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;
t.show();
q = (Q2)t;
q.show();
}
}
ans is
C) prints "Show method in Test class"...1
"Show method in Q2 class".....2
"Show method in Test class"...3
"Show method in Q2 class".....4
shouldn't the ans be line 1,2,4,3 bcoz then what is the meaning of assigning t=q ?doesn't t.show now mean q.show?


[This message has been edited by hema janani (edited August 24, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Hema,
Remember that static methods are not overridden and hence there is no polymorphic behavior. If you use a reference to invoke a static method, the method corresponding to the type of the reference is called. In the code, using t ( type Test) calls the method in Test, using q(type Q2) calls the method in Q2 and so on.
Vani
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
may be I am wrong for asking you this question at this time but I got confused .....in the code above why it's not giving any ClassCastException at q=(Q2)t....when I was experimenting such type of examples by taking one parent class and a subclass I am getting the error when I tried to do that..here I am giving my simple example I tried...
class parent {}
class sub extends parent{}
public static void main(String args[])
{
parent p=new parent();
sub s= new sub();
p=s //compiles and runs fine
s=(sub)p //giving classcastexception at run time..
}
I think the same thing done in the above code..please let me know.
Krishna
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the point to notice here is not the hiding of static methods but the fact that
"When you pass an object reference variable to a function, you can potentially change the state of the object, but you can't make the passed reference variable refer to another object. "
as mentioned by DaB here http://www.javaranch.com/ubb/Forum24/HTML/002279.html
U can visit this post for in-depth understanding. Well it helped me get the logic and it works perfectly for the code given by Hema.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too have the same doubt as Krishna. I expected a class cast exception at runtime in
q = (Q2)t;
Since t is of type parent, how come it is not giving class cast exception at runtime?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above given code would not give ClassCastException when the Super class & Sub class are directly related -- The below statements from the Java API should clear your doubt:
"Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);
System.out.println((String)x);
"


Originally posted by Baskaran Subramani:
I too have the same doubt as Krishna. I expected a class cast exception at runtime in
q = (Q2)t;
Since t is of type parent, how come it is not giving class cast exception at runtime?


 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic