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 use of super -Examlab Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "use of super -Examlab Question" Watch "use of super -Examlab Question" New topic
Author

use of super -Examlab Question

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Source:Examlab

i want to ask few questions o the above code
1) what is the use of super at line 1 why the output is DemoC# instead of DemoJava
2) what we are trying to access using super.s
3)why the output is same even though we use super.s ,this.s ,or s in println statement at line 1

Thanks & Regards
Sumit Kothalikar
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

sumit kothalikar wrote:
i want to ask few questions o the above code
1) what is the use of super at line 1 why the output is DemoC# instead of DemoJava

super is used to invoke the super class members, such as, methods, instance variables. If you redefine the instance variables of the super class in your sub class, then you can invoke the super class members using super. But, here, even though, you've invoked with the super keyword, because, you've re-assigned the values for the instance variables, the re-assigned value will be there in the super class instance variable!
sumit kothalikar wrote:
2) what we are trying to access using super.s

As said earlier, the super class version of the instance variable s. But, you've re-assigned the value!
sumit kothalikar wrote:
3)why the output is same even though we use super.s ,this.s ,or s in println statement at line 1

I think, it should be clear now!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


Hi Guys,

Worth mentioning here is order on which code gets executed while creating object.

1) In this line XInterface xi=new YClass(); , YClass constructor gets called which before executing anything call XClass() constructor which first execute code on class body with top to bottom approach and created object String s="java" after XClass() constructor completes control backs to YClass() constructor which again first execute code on class body from top to bottom and reassign s="C#" , that's why when you refer s (no matter what you used , this, super etc) you will get final value "C#"


SQL Query Interview Question
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
I don't quite get it...

isn't the instance initialization block in Y assigning "C#" to the s variable that Y inherited?

So Y's s would be "C#", and X's s would be "C++".

Calling super.s() would return "C++".

Why is this not the case?


OCA, OCP Java 6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Chandella Montero wrote:
Calling super.s() would return "C++".


how? after sub class instantiated now the value of s is c# . accessing super.s refers "C#" .
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
I though the superclass would have an s, and the subclass would have its own s, through inheritance. Or not?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Chandella Montero wrote:I though the superclass would have an s, and the subclass would have its own s, through inheritance. Or not?

Nope. the instance variable belong to super class object and subclass can access/use that variable.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Now why doesn't it inherit it? It's an instance variable.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Chandella Montero wrote:Now why doesn't it inherit it? It's an instance variable.


See my comments and analysis below for more info....reread the topic if you still have doubt. Hope it help...



OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of super -Examlab Question