• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism

 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
int x=5;
}

class B extends A
{
int x=6;
}



class covtest
{

public A getObject()
{
System.out.println("its A from covtest");
return new A();
}


public static void main(String arg[])
{
covtest t = new subcovtest();
System.out.println(t.getObject().x); // *
}
}



class subcovtest extends covtest
{
public B getObject()
{
System.out.println("its B from subcovtest");
return new B();
}
}
the output is:
its B from subcovtest
5;
expected output is:
its B from subcovtest
6;
please explain,why?
 
Frank Kellinghusen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code us unreadable:

http://faq.javaranch.com/java/UseCodeTags
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Kellinghusen wrote:Your code us unreadable:

http://faq.javaranch.com/java/UseCodeTags
It's not much better without indentation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Kellinghusen, welcome to JavaRanch

Cchetan Jain, remember that polymorphism doesn't apply to fields, nor to static members.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t.getObject() will be decided at run time while t.getObject().x will be decided at compile time. Because polymorphism is appicable only for instance mehtod.
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello shivendra tripathi..please explain what is the concept behind the t.getObject().x;
when t.getObject() returns B then simply it must return 6..i m confused with this..
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat:
Campbell Ritchie wrote:remember that polymorphism doesn't apply to fields, nor to static members.


When accessing fields or static methods, the JVM uses the reference type. Since t's reference type is covtest, it's getObject() method return type is A, not B*. Therefore, A's field x is used.

* The actual type (as returned by getClass()) is B, but the declared type is A.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:* The actual type (as returned by getClass()) is B, but the declared type is A.
I thought it would be something like that.

Rob is very good about that sort of thing. Thank you, Rob
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your wisdom...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic