Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of super -Examlab Question

 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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#"
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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#" .
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I though the superclass would have an s, and the subclass would have its own s, through inheritance. Or not?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
T Vergilio
Ranch Hand
Posts: 230
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now why doesn't it inherit it? It's an instance variable.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic