• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

over ridden method and super constructor

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



When I run this code it prints output "This is test method in sub class"

Why is it so ? SuperTest sup is parent class to SubTest. It has access to methods defined in super class. How come its accessing methos in a child class ? Accrding to my understanding it should print "This is test method in super class". PLease explain, why is a super class reference accessing method of a subclass ?




 
Ranch Hand
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nirjari,

It is because the Instance you have created is of the sub class not the super class.
sup is just a reference of type super class which is actually pointing to the instance of the sub class.

Hope it helps .

cheers,
Saurav
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to extend SuperTest
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how were you able to compile this code, there is no method SuperTest()?
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SuybTest declaration is as follows.

"public class SubTest extends SuperTest"

It is because the Instance you have created is of the sub class not the super class.
sup is just a reference of type super class which is actually pointing to the instance of the sub class.



I understand that instance created is of sub class. But reference type is of SuperTest which is super class. So this reference will have access to methods of super class only. In that case, it should access
testMethod() available in SuperTest.

Please explain.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you override a method in a parent class you do not need to create an instance of the parent class and call it, you simply say

to use the child method


so


is unnecessary, all you really need is:

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nirjari,


Actually the method to be called at runtime is dependent on the object instance rather than an object references. So because you are using the subclass instance, the subclass method is being called, although at compile time, the superclass method is being checked for syntax. It is for the same reason that if your superclass method was written as:



then the statement:



would not compile, as the complier would be matching it to the superclass reference.

And guess what?? If you did include the proper try and catch statement, the ouput would be "This is test method in sub class"

as the sublass instance is being used to determine the overriden method to be run at runtime.!!!
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic