• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

doubt on inheritance ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a subclass inherits a method from superclass(and does not override it), does that mean that method has been copied over to the subclass. i.e. does subclass HAS that method. put it in other way does subclass has declaration of the inherited method ??
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:when a subclass inherits a method from superclass(and does not override it), does that mean that method has been copied over to the subclass. i.e. does subclass HAS that method. put it in other way does subclass has declaration of the inherited method ??



Inheritance means code reusability,so i think yes code will be copied at run time from superclass .
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply Saloni. but if we say assume as you say then it means the method code of the superclass will be copied over to subclass. now just take a scenario that the superclass has private instance variables and the method which we are inheriting from the superclass initializes those instance members . something like this




so if we think that the method gets copied in the subclass, it won't make any sense for the instance variables for Sub class, since those instance variables are not even visible in Sub class and hence cannot be accessed.
if what i think is right, that means there is no COPYING OVER . the inherited method gets called through superclass declaration. lets wait for expert to post ?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:thanks for the reply Saloni. but if we say assume as you say then it means the method code of the superclass will be copied over to subclass. now just take a scenario that the superclass has private instance variables and the method which we are inheriting from the superclass initializes those instance members . something like this




so if we think that the method gets copied in the subclass, it won't make any sense for the instance variables for Sub class, since those instance variables are not even visible in Sub class and hence cannot be accessed.
if what i think is right, that means there is no COPYING OVER . the inherited method gets called through superclass declaration. lets wait for expert to post ?



I think so because if you think it isn't not copied here and it is called then what do you will say about this super.play(x,y) ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

gurpeet singh wrote:thanks for the reply Saloni. but if we say assume as you say then it means the method code of the superclass will be copied over to subclass. now just take a scenario that the superclass has private instance variables and the method which we are inheriting from the superclass initializes those instance members . something like this




so if we think that the method gets copied in the subclass, it won't make any sense for the instance variables for Sub class, since those instance variables are not even visible in Sub class and hence cannot be accessed.
if what i think is right, that means there is no COPYING OVER . the inherited method gets called through superclass declaration. lets wait for expert to post ?



I think so because if you think it isn't not copied here and it is called then what do you will say about this super.play(x,y) ?




even super.play(x,y) will call the method from the superclass. it is again my thinking that method is declared only in superclass. subclass inherits it but it is not COPYING. when we invoke the play method from Subclass object it is called through superclass class declaration.
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

gurpeet singh wrote:thanks for the reply Saloni. but if we say assume as you say then it means the method code of the superclass will be copied over to subclass. now just take a scenario that the superclass has private instance variables and the method which we are inheriting from the superclass initializes those instance members . something like this




so if we think that the method gets copied in the subclass, it won't make any sense for the instance variables for Sub class, since those instance variables are not even visible in Sub class and hence cannot be accessed.
if what i think is right, that means there is no COPYING OVER . the inherited method gets called through superclass declaration. lets wait for expert to post ?



I think so because if you think it isn't not copied here and it is called then what do you will say about this super.play(x,y) ?





Correct.... there is no copying of code -- either at runtime or at compile time. When an object is instantiated, a jump table is created which points to all the locations of the methods, and if a subclass didn't override the method, the table entry for that method will be at the super class (or its super class, depending on the location of the last class which overriden the method)..... of course, we are only talking about instance methods which are accessable.

Henry
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Correct.... there is no copying of code -- either at runtime or at compile time. When an object is instantiated, a jump table is created which points to all the locations of the methods, and if a subclass didn't override the method, the table entry for that method will be at the super class (or its super class, depending on the location of the last class which overriden the method)..... of course, we are only talking about instance methods which are accessable.

Henry



It means method will be called.Key point was private variables that is enough to know about this.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

saloni jhanwar wrote:

gurpeet singh wrote:thanks for the reply Saloni. but if we say assume as you say then it means the method code of the superclass will be copied over to subclass. now just take a scenario that the superclass has private instance variables and the method which we are inheriting from the superclass initializes those instance members . something like this




so if we think that the method gets copied in the subclass, it won't make any sense for the instance variables for Sub class, since those instance variables are not even visible in Sub class and hence cannot be accessed.
if what i think is right, that means there is no COPYING OVER . the inherited method gets called through superclass declaration. lets wait for expert to post ?



I think so because if you think it isn't not copied here and it is called then what do you will say about this super.play(x,y) ?





Correct.... there is no copying of code -- either at runtime or at compile time. When an object is instantiated, a jump table is created which points to all the locations of the methods, and if a subclass didn't override the method, the table entry for that method will be at the super class (or its super class, depending on the location of the last class which overriden the method)..... of course, we are only talking about instance methods which are accessable.

Henry



Henry, what about the instance variables ? are they copied to the subclass class declaration or they too are accessed in the same way as instance methods ?
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:
Henry, what about the instance variables ? are they copied to the subclass class declaration or they too are accessed in the same way as instance methods ?



Gurpeet,

Since you were able to write an application, to figure out, and to prove your previous point as correct, take a shot at testing instance variables. Write a program that can be used to test whether instance variables are copied from the super class, or if the variable accessed from the subclass scope is the same one access from the superclass scope.

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

gurpeet singh wrote:
Henry, what about the instance variables ? are they copied to the subclass class declaration or they too are accessed in the same way as instance methods ?



Gurpeet,

Since you were able to write an application, to figure out, and to prove your previous point as correct, take a shot at testing instance variables. Write a program that can be used to test whether instance variables are copied from the super class, or if the variable accessed from the subclass scope is the same one access from the superclass scope.

Henry



yes henry the same thing holds for instance variables too. there is just one copy of the inherited instance methods and variables and they are accessed using the superclass class declaration. there is NO copying of methods or instance variables.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you craete an Object say A(extends B) , there is only one object created(A) and his ancestor properties are stored in that object via ancestor public methods.
[I am not that confident here ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic