Results 1 to 4 of 4

Thread: super

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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; //?
      }
    }

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: super

    Here is what I'v found to solve this problem:

    Java Code:
    public class TestSon {
      public static void main(String[] args) {
        Son s = new Son();
        int sonsPrc = s.prc;
        int fathersPrc = ((Father)Son).prc; //?
      }
    }
    So, seems it's possible to access Father's prc from an instance of Son.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: super

    Why not:
    int fathersPrc = Father.prc;

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: super

    Quote Originally Posted by Norm View Post
    Why not:
    int fathersPrc = Father.prc;
    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 11:58 PM.

Similar Threads

  1. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 07:12 AM
  2. this() and super() ?
    By bob in forum New To Java
    Replies: 4
    Last Post: 08-13-2009, 05:56 PM
  3. super
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 01-23-2009, 04:07 PM
  4. this() vs super()
    By Hevonen in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 02:43 PM
  5. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •