Results 1 to 8 of 8
  1. #1
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default calling variable using super super..

    Java Code:
    Class A
    {
    int i;
    }
    class B extends A
    {
    int i;
    }
    class C extends B
    {
    int i;
    }
    I can use the variable i in class B using super.. as super.i
    My question is how would i call variable i of class A in class C.
    The Quieter you become the more you are able to hear !

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    I believe you can't. You could make a get method.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I think the best option, like Aziz said, is to make a get method in B, that returns super.i... having said that, I'm curious as to why you would ever need this. Think about the actual fundamentals of OOP design; if C needs to access A, then it's not really a subclass of B.. it's more of a direct subclass from A.

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    PHP Code:
    package javaapplication36;
    
    /**
     *
     * @author alacn
     */
    public class Main {
    
         int  i;
    
    public Main(){
    
            i = 5;
            System.out.println(i);
    }
        public static void main(String[] args) {
    
            NewClass1 newclass1 = new NewClass1();
        }
    
    }
    PHP Code:
    package javaapplication36;
    
    /**
     *
     * @author alacn
     */
    public class NewClass extends Main {
    
        public NewClass(){
    
            super.i = 6;
            System.out.println(i);
    
        }
    
    }

    PHP Code:
    package javaapplication36;
    
    /**
     *
     * @author alacn
     */
    public class NewClass1 extends NewClass{
    
       NewClass1(){
    
    
          super.i = 7;
          System.out.println(i);
       }
    }
    output 5,6,7
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    I think the best option, like Aziz said, is to make a get method in B, that returns super.i... having said that, I'm curious as to why you would ever need this. Think about the actual fundamentals of OOP design; if C needs to access A, then it's not really a subclass of B.. it's more of a direct subclass from A.
    what about if the majority of the variables class C inherited was found in class B and one or two was found in class A. Even though class C is accesing class A directly, surely it makes sense for it to access class A through class b to avoid duplicating all the variables declared in class B?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Yes and no. Class C might be an extension of A indirectly, through B, but if something that's part of A is accessed by C, there has to be an ambiguous step in the middle. I can try to explain this further when I'm back from vacation and have NetBeans at my disposal.

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Yes and no. Class C might be an extension of A indirectly, through B, but if something that's part of A is accessed by C, there has to be an ambiguous step in the middle. I can try to explain this further when I'm back from vacation and have NetBeans at my disposal.
    to be honest i do agree with you simply because i would rather access a variable via a method rather than directly for encapsulation purposes.

    edit

    however im new to the concept of encapsulation in java, so my reasons may be flawed
    Last edited by alacn; 08-16-2010 at 05:58 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  8. #8
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    Guys I was asked this question on my viva. I just wanna know if there is any way to "directly" access the variable in Class A through Class C without using "get" method or say without intervention of Class B!!
    The Quieter you become the more you are able to hear !

Similar Threads

  1. this() and super() ?
    By bob in forum New To Java
    Replies: 4
    Last Post: 08-13-2009, 05:56 PM
  2. this() vs super()
    By Hevonen in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 02:43 PM
  3. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 09:22 PM
  4. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 07:37 PM
  5. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:52 AM

Posting Permissions

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