File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Puzzled output for a program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Puzzled output for a program" Watch "Puzzled output for a program" New topic
Author

Puzzled output for a program

vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17
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

Joined: Oct 24, 2010
Posts: 557
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?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 03, 2008
Posts: 4523
    
    5

vicky, please BeForthrightWhenCrossPostingToOtherSites
http://in.answers.yahoo.com/question/index?qid=20120128015214AA1btS6


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
And welcome to the Ranch
vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17



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

Joined: Aug 05, 2005
Posts: 3169
    
  10
vicky ece wrote:



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.


Joanne
vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2012
Posts: 17
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
vicky ece wrote: . . . Thank you very much yes i understand....
Well done getting it and well done Jeff explaining it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

vicky ece wrote:
Thank you very much yes i understand....


Cool. I'm glad you got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Puzzled output for a program
 
Similar Threads
question on NumberFormat class
Why this Output?
Overloaded Methods in Java are early binded or late binded
question on object oriented concepts
Generics ... help ! ?