1)here in this program when i say obj . display () then the derived class display is being overridden and it gets invoked ..here when iam saying obj . display () then display () method uses base class a (instance members )or derived class a (instance members ) ?
2)similarly there is a statement in java that instance members are not overridden
but it is not applying here when i say obj.a ,obj.b then it is displaying 4and 5 rather than 2 and 3 ...
but we know that instance members are not overridden ? then why it happens like this? but here derived class instance members are overridden ???
3) when i say a obj = new b(); then base class instance members come into picture ..is it due to type of obj ? Does that mean that instance members are not overridden. applies only basing on their type ?? and instance methods are overridden irrespective of their type ? like obj.display() invokes derived class methods without bothering about their type?
4) JVM starts walking up the inheritance tree starting at the classtype you invoked the method on ? But what happens if jvm doesnt find a match
What does these words mean ? can you explain it briefly?
5) when i say a obj = new b(); then the jvm will search for the method in b's class or first in a's class .confused about these things answer if possible as if the compiler and jvm thinks ...I mean to say that will the jvm will go and search which class first in inheritance?
6) what s the meanig of return key ? what does it indicate? how to use that in our program?
provide me an apt link atleast if you dont find time to answer my queries ?
SCJP 6
hansika motwani wrote:1)class a
2){
3)int a =2,b=3;
4)void display()
5){
6)System.out.println(a);
7)}
8)}
9)class b extends a
10){
11)int a =4,b=5;
12)//void display()
13)//{
14)//System.out.print(b);
15)//}
16)}
17)class c
18){
19)public static void main(String args[])
20){
21)b obj = new b();
22)obj.display();
23)System.out.println(obj.a);
24)System.out.println(obj.b);
25)}
26)}
Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?
Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...
same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.
then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?
hansika motwani wrote:
hansika motwani wrote:1)class a
2){
3)int a =2,b=3;
4)void display()
5){
6)System.out.println(a);
7)}
8)}
9)class b extends a
10){
11)int a =4,b=5;
12)//void display()
13)//{
14)//System.out.print(b);
15)//}
16)}
17)class c
18){
19)public static void main(String args[])
20){
21)b obj = new b();
22)obj.display();
23)System.out.println(obj.a);
24)System.out.println(obj.b);
25)}
26)}
Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?
Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...
same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.
then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?
Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?
SCJP 6
Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...
same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.
then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?
here correct me if iam wrong then obj.b will call a.b variable na ...
then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?
if the type is a then also b's instance vairable is getting invoked . why so ?
SCJP 6
hansika motwani wrote:SORRY FOR THE MISTAKE .I WRONGLY POSTED IT THREE TIMES..HOW TO delete ..how to use code tags ? convey it please
Jesper Young wrote:Please use code tags when you post source code.
Have you no shame? Have you no decency? Have you no tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|