• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OVERRIDING ISSUES

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers help me in overriding issues !



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 ?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



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.
instance variable a is called on reference type, and decided at compile time by compiler, here actual reference type is b as you have used b obj; so b.a will be called, if you had used a obj=new b(); than obj.b will call a.a variable.


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



Instance members a & b of class are shadowed by instance member a & b of class b, this is called hiding or shadowing concept, now overridden, overriding depends on polymorphism and which method to call is decide by jvm at runtime by and jvm uses actual object type not the reference type.
While in this case which instance member to call is not polymorphism, not decided by jvm, it is decided by compiler at compile time by only seeing the reference type not the actual object type, same applies for static methods.

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?


Ya it means instance members are not overridden they are hidden actually.

if you use
a obj=new a();
obj.display() will call a's method as you are using new a();
a obj=new b();
obj.display() will call b's method as you are using new b();


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?



a obj=new b();
obj.display();


here we used new b(), so it will find display() in class b first, if it does not find there then it will go up in inheritance tree and will find in class a.

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?



I think above question was the same as this.

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 ?



I think return key means return type of your method. Please give complete sentence where you find this words used.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
hansika motwani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SORRY FOR THE MISTAKE .I WRONGLY POSTED IT THREE TIMES..HOW TO delete ..how to use code tags ? convey it please
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is code button just beside Quote button.

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 ?



Simply remember, base class methods cannot see derived class's variables. Here class b is using class a's display() method, but that display() method is declared only in class a, so it cannot see anything declared in class b. Just write small samples to clear it.



 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?



Ya you are right here :

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 ?



I think you have this doubt.

if the type is a then also b's instance vairable is getting invoked . why so ?



Are you using this code:



this will call b' display() method as you have used new b(); and in display() method you have used


this means you are using:

And here this=new b(), so the type of b is being used in printing this.b not the type of a.


 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi are you hansika Motwani, the actress. From when have you started learning Java? Please let me know
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Note that there's a link in my message. Click the link to learn how to use code tags.

Also note that you see an edit button at the top right of your own posts, which you can click to edit your posts.
 
hansika motwani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stop knowing about personal details and involve in the subject .utilize your valuable time for java discussions rather about personal issues ..This is a good forum which is very rare for java lovers ..so please focus on java..Its my sincere appeal
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we moderators take the naming policy seriously here at JavaRanch. Your display name must be a real-sounding name, and not something obviously fictitious, such as the name of a movie star.

So, please change your display name. This is not optional - if you don't, we might close your JavaRanch account.
 
Raza Khan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hansika what is your real name? contact me on irfan_loves_all@yahoo.com... ok ...and are you a btech or Mca?
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic