File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes OVERRIDING ISSUES Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "OVERRIDING ISSUES" Watch "OVERRIDING ISSUES" New topic
Author

OVERRIDING ISSUES

hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
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 ?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
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

Joined: Jul 22, 2008
Posts: 44
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

Joined: Jul 22, 2008
Posts: 44
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

Joined: Jul 22, 2008
Posts: 44
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Oct 16, 2008
Posts: 952
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.


Raza Khan
Greenhorn

Joined: Nov 19, 2008
Posts: 15
Hi are you hansika Motwani, the actress. From when have you started learning Java? Please let me know
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Jul 22, 2008
Posts: 44
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Nov 19, 2008
Posts: 15
Hansika what is your real name? contact me on irfan_loves_all@yahoo.com... ok ...and are you a btech or Mca?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OVERRIDING ISSUES
 
Similar Threads
Static methods & Overriding!
this in java
no return
final transient and static transient
is static variables serialized?