File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriden method - Output not as expected

 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

as far as I know the Object type determines which OVERRIDEN method will be used at runtime. So I tried out the code below:



So its a legal override and when I run the above code the output is "Super Ball"

I was expecting "Sub Ball" as the object type is Minor and the object type determines the overriden method

I thought I had these type of questions fully understood as I was getting the questions correct but this has really caught me out

Thanks
 
Paul Campbell
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you didn't actually override the method since it is marked private in Major.

if you remove the private access modifier from your method within Major, you will get the results you expected.
[ November 17, 2008: Message edited by: Paul Campbell ]
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private methods can not be overridden by subclasses
and inheritance not works with them
therefore object type is not important...


[ November 17, 2008: Message edited by: Anut Walidera ]
 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh!

yes its a private method and is not overriden


So the reference type determines the call for private methods.

Thanks to all for pointing this out
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic