Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Puzzled output for a program

 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the below code i get A as output . My question here is that since i am having the object of type "B" why it is printing A as output . Ya i know that here overriding is not present since dosomething() in A is private but still the question is since i have the object of type "B" how can it print A and that too class A is abstract so there is no way to create a object of type "A" . Any Idea please



 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can play around with this code a bit yourself and draw some conclusions. For example, what happens if you change the type of a to B?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky ece wrote:class A is abstract so there is no way to create a object of type "A" .


That's not quite true. While can't simply construct an A directly, if B is a subclass of A, then every B IS-AN A. Obviously, abstract classes' instance methods have to be able to be called, otherwise, what would be the point of abstract classes in the first place?

So, in this case, we have a reference that is declared to be of type A, and we are calling its doSomething() method. Since that method is priate, we know it won't be overridden, so which implementation of that method to call is determined at compile time--A's implementation. This is all decided at compile time.

At runtime, we create an instance of concrete class B, and we store a reference to it in variable "a", which is declared to be of type A. The fact that A is abstract does not matter here.

And welcome to the Ranch!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky, please BeForthrightWhenCrossPostingToOtherSites
http://in.answers.yahoo.com/question/index?qid=20120128015214AA1btS6
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:vicky, please BeForthrightWhenCrossPostingToOtherSites
http://in.answers.yahoo.com/question/index?qid=20120128015214AA1btS6



Sorry for Posting the same question in another site . i have not got the answer from that site so i done this one...........
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky ece wrote:
Darryl Burke wrote:vicky, please BeForthrightWhenCrossPostingToOtherSites
http://in.answers.yahoo.com/question/index?qid=20120128015214AA1btS6



Sorry for Posting the same question in another site . i have not got the answer from that site so i done this one...........

Posting the same question on another site is not a problem. It just helps if you include a link to it here so people can see what answers have already been given. That way people here don't waste their time giving you the same answers.
And of course you should post a link to here on the other site so people there don't waste their time either.
 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in this case, we have a reference that is declared to be of type A, and we are calling its doSomething() method. Since that method is priate, we know it won't be overridden, so which implementation of that method to call is determined at compile time--A's implementation. This is all decided at compile time.

At runtime, we create an instance of concrete class B, and we store a reference to it in variable "a", which is declared to be of type A.

"The fact that A is abstract does not matter here."
Ya i understand ..
Thanks for you explanation...
but if a method call is decided at compile time then why this giving me null pointer exception....


Here since i don't have a object it is giving me an null pointer exception but in the below code since i have B object how can i call A class Method....




Thanks in advance

 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have actually already given the answer in the other forum yourself. As Joanne said, you ought to post a link to this discussion on this forum.
The method you are calling is private, and only accessible because you have the main method in the same class. Because it is private, it is not inherited, so the B class doesn’t inherit that method, so you cannot see polymorphism.
. . . accessible members of a superclass or superinterface which are neither private nor hidden nor overridden . . .

[from the link I quoted]
If that method is private it is not inherited, so the doSomething() calls have to go with the declared type of the object. When you declare it as an A, you get the A version.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky ece wrote:
but if a method call is decided at compile time then why this giving me null pointer exception....



Because a is null and you can't dereference a null pointer. Also, your phrase "a method call is decided at compile time" is incomplete and not applicable here.

What's decided at compile time is which signature to call. Additionally, if the method is static or private or final, then which class's implementation is decided at compile time. That doesn't mean which object to call it on is decided at compile time. You seem to be confusing class with object here. There's no object, and a non-static method requires an object.

Here since i don't have a object it is giving me an null pointer exception but in the below code since i have B object how can i call A class Method....




I explained that in my first response. The B IS-AN A, so we have an A object. Variable a is of type A, and doSomething() is private, so it's not polymorphic, so we get A's version.
 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
vicky ece wrote:
but if a method call is decided at compile time then why this giving me null pointer exception....



Because a is null and you can't dereference a null pointer. Also, your phrase "a method call is decided at compile time" is incomplete and not applicable here.

What's decided at compile time is which signature to call. Additionally, if the method is static or private or final, then which class's implementation is decided at compile time. That doesn't mean which object to call it on is decided at compile time. You seem to be confusing class with object here. There's no object, and a non-static method requires an object.

Here since i don't have a object it is giving me an null pointer exception but in the below code since i have B object how can i call A class Method....




I explained that in my first response. The B IS-AN A, so we have an A object. Variable a is of type A, and doSomething() is private, so it's not polymorphic, so we get A's version.




Thank you very much yes i understand....
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky ece wrote: . . . Thank you very much yes i understand....
Well done getting it and well done Jeff explaining it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky ece wrote:
Thank you very much yes i understand....


Cool. I'm glad you got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic