• 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

use of super -Examlab Question

 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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#"
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now why doesn't it inherit it? It's an instance variable.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic