Results 1 to 8 of 8
- 08-15-2010, 08:57 PM #1
calling variable using super super..
I can use the variable i in class B using super.. as super.iJava Code:Class A { int i; } class B extends A { int i; } class C extends B { int 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 !
- 08-15-2010, 10:56 PM #2
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)
- 08-16-2010, 12:30 AM #3
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.
- 08-16-2010, 04:17 AM #4
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); } }
output 5,6,7PHP Code:package javaapplication36; /** * * @author alacn */ public class NewClass1 extends NewClass{ NewClass1(){ super.i = 7; System.out.println(i); } }Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-16-2010, 04:35 AM #5
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
- 08-16-2010, 04:49 AM #6
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.
- 08-16-2010, 04:54 AM #7
Last edited by alacn; 08-16-2010 at 04:58 AM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-16-2010, 06:12 AM #8
Similar Threads
-
this() and super() ?
By bob in forum New To JavaReplies: 4Last Post: 08-13-2009, 04:56 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 -
Super CSV 1.15
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-16-2007, 06:37 PM -
Use super. or this.
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks