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 ?
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 ?