Results 1 to 4 of 4
Thread: super
- 01-29-2012, 09:00 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
super
"You can reference the hidden data field or static method using the keyword super in the subclass."
And than, I read: "The keyword super can be used in two ways: To call a superclass constructor. To call a superclass method".
And that is all ok, till I used keyword super to access superclass data field(instance or class/static).
My question is, is there a way to access Father's prc(instance or static), from an instance of Son?
Say:
Java Code:public class Father { public static int prc; } public class Son extends Father { private int prc; public Son() { super.prc = 1; } }Java Code:public class TestSon { public static void main(String[] args) { Son s = new Son(); int sonsPrc = s.prc; int fathersPrc; //? } }
- 01-29-2012, 09:43 PM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: super
Here is what I'v found to solve this problem:
So, seems it's possible to access Father's prc from an instance of Son.Java Code:public class TestSon { public static void main(String[] args) { Son s = new Son(); int sonsPrc = s.prc; int fathersPrc = ((Father)Son).prc; //? } }
- 01-29-2012, 10:06 PM #3
Re: super
Why not:
int fathersPrc = Father.prc;
- 01-29-2012, 10:49 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: super
In case fathersPrc is static field, it is ok.
But if it is instance field, instance of Son have inherited fathersPrc.
And need type casting to access that inherited data field from instance of Son.
As long as Son extends Father.
As I understand, in this case type casting behaves as a "colander".Last edited by diamonddragon; 01-29-2012 at 10:58 PM.
Similar Threads
-
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
this() and super() ?
By bob in forum New To JavaReplies: 4Last Post: 08-13-2009, 04:56 PM -
super
By dj kourampies in forum New To JavaReplies: 5Last Post: 01-23-2009, 03:07 PM -
this() vs super()
By Hevonen in forum New To JavaReplies: 9Last Post: 12-04-2008, 01:43 PM -
Super CSV 1.20
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks