• Post Reply Bookmark Topic Watch Topic
  • New Topic

suggest the output and explanation for it  RSS feed

 
Roshi Kumar
Greenhorn
Posts: 21
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
01 public class Parent{
02 protected int myMethod(){
03 return 5;
04 }
05 public static void main(String[] args)
06 {
07 Parent myParent = new Child();
08 System.out.println(myParent.myMethod());
09 }
10 }
11 class Child extends Parent{
12 public int myMethod(){
13 return 55;
14 }
15 }
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you run it yourself and see what happens.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us where this is from?

also, when you post code here, please don't include line numbers. Having them on there makes it difficult for someone to cut-n-past it into an editor to see what it would do. Instead, paste the plain java between code tags:

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Why don't you run it yourself and see what happens.

Well said, Tony
Assailant Roshi, what do you infer from this code ? and if that inference does not augur with your inference then you should try to find out why?
This in fact should be the question
 
Roshi Kumar
Greenhorn
Posts: 21
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it n got output as 55 .

SubClass method will be called even if reference type is SuperClass .. N Access Modifier can be less restricted in overridden class.

Thanks for your replies.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assailant Roshi wrote:SubClass method will be called even if reference type is SuperClass

remember that (at least in java), the reference variable does not hold the actual object...it just kind of points to it. So when you call myParent.myMethod(), you're telling the JVM to "go to the object that myParent is pointing to, and run its myMethod method."

Since the actual object created is a Child, then it makes perfect sense that the Child version of myMethod would run.
 
Roshi Kumar
Greenhorn
Posts: 21
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:

Since the actual object created is a Child, then it makes perfect sense that the Child version of myMethod would run.


But in case of Overloading it may refer to SuperClass method and not SubClass method . Please correct me if wrong here .

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in case of Overloading it may refer to SuperClass method and not SubClass method

Overloading and overriding are different things altogether. With overloading the visible method with a parameter signature that matches the parameters in the call will be used (note widening, boxing, up-casting etc may be used to find a matching signature if an exact match can't be found), if that method happens to be in a super class and it hasn't been overridden by a sub class then the super classes method will be called.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!