Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

over ridden method and super constructor  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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 ?




 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to extend SuperTest
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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:

 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!