"If the facts don't fit the theory, get new facts" --Albert Einstein
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-
In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-
In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??
Hello.......
Superclass code along with subclass code is loaded into class loader.
Declaring the instance variable with the same name in subclass overrides the instance variable of superclass.
To refer to super class members use the keyword super.
astinct ast wrote:
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-
In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??
private members are not accessible outside its class and this applies to superclass too.
When you will run the above code.you will find the output to be 20 and not 50 because java runtime system will first check the instance of i in subclass.If it is there i will be printed otherwise it will refer to superclass for i.
Now remove i from subclass and run the code ,you will get an error because i is declared to be private in superclass and private members are not accessible outside its class.
astinct ast wrote:
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-
In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??
Hello.......
Superclass code along with subclass code is loaded into class loader.
Declaring the instance variable with the same name in subclass overrides the instance variable of superclass.
To refer to super class members use the keyword super.
Suppose there is a variable 'i' declared into both the super class and subclass. then to which 'i' the inherited code of the superclass will refer to ??
I read somewhere that we private members are of the super Class are not inherited. So what does it mean? Are they not inherited or they are inherited but are hidden into the subclass.
Learning starts with a question and continues with a discovery Stay hungry for knowledge.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |