Results 1 to 3 of 3
- 07-18-2007, 05:11 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
I have a problem with variable "i"
I have a problem. Why does the program ask me for the variable "i" three times, and how could I fix it?
Sorry for making it so long, but I don't know any other way of illustrating my problem..
Java Code:import java.util.*; public class Test { public static void main (String args[]) { Testing1 T; T = new Testing1(); T.Testing1Method(); Testing2 T2; T2 = new Testing2(); T2.Testing2Method(); Testing3 T3; T3 = new Testing3(); T3.Testing3Method(); } }Java Code://Testing1.java import java.util.*; public class Testing1 { Scanner scan = new Scanner(System.in); int i = scan.nextInt(); public void Testing1Method() { System.out.println("Test1= " + i); Testing2Method(); } public void Testing2Method() { } }Java Code://Testing2.java public class Testing2 extends Testing1{ public void Testing2Method(){ super.Testing2Method(); System.out.println("Test2 " + i); Testing3Method(); } public void Testing3Method() { } }ThanksJava Code://testing3.java public class Testing3 extends Testing2 { public void Testing3Method() { super.Testing3Method(); System.out.println("Test3 " + i); } }
- 07-20-2007, 06:43 AM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
For the problem of why the program ask u for the variable i 3 times, it involves the logical steps the compiler take to compile ur code
U can see that in each class, u didn't include a constructor, so the compiler creates a default constructor for u during the compilation process, usually the default constructor won't do anything, but in this case, somehow it created a default constructor that consists of something like thisJava Code:Testing1 T; T = new Testing1();//create an object of testing1 T.Testing1Method(); Testing2 T2; T2 = new Testing2();//create an object of testing2 T2.Testing2Method(); Testing3 T3; T3 = new Testing3();//create an object of testing3 T3.Testing3Method();
Testing3 are inherited from Testing2 which were inherited from Testing1Java Code:public Testing1//the Testing1 default constructor { Scanner scan = new Scanner(System.in); int i = scan.nextInt();//it'll ask for an input }
Each time u create an object from a class that was inherited from another class (..lets use Testing2 for example..),
if u don't manually include and write a constructor for ur inherited class (..which is Testing2..), the compiler won't go straight into making u a default constructor for the inherited class, but instead, it will look into the parent class 1st (..Testing1 in this case..) and check if the parent class has a constructor that it can use as the constructor of the inherited class (..Testing2..), if the compiler found no available constructor after searching all of the available parent classes, then it'll create a default constructor.
If i'm not mistaken, u created 3 objects, each was an instance of Testing1, Testing2 and Testing3. But since there's no constructor for each classes, the compiler generate a same default constructor to create each object, which was the default constructor for Testing1 that i just wrote above, which as u know, asked for an input, thats why it asked for the 'i' variable 3 times :D
If i may ask, what was the code supposed to do actually?:confused:
Im not really sure, but judging from the source code u just written, i presume u r trying to do some examples about either polymorphism or overriding/overloading methods with an output of this sort maybe?
:confused:Java Code:<type an input for the variable 'i' here, ex : 5> Test1 = 5 Test2 = 5 Test3 = 5 <another input for 'i', ex : 4> Test2 = 4 Test3 = 4 <3rd input for 'i', ex : 6> Test3 = 6
- 08-07-2007, 11:05 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
All the problem is in your access modifier for 'i';
It is so called Default - visible only from within file where class is defined and not accessible for others. If you came from VB - this could be the case that you would assume that default access modifier should be Public if you don't give one... But it is not.... Change to Public or Protected and it will work just fine.
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
"Displayed tab width" problem...
By Petike in forum EclipseReplies: 0Last Post: 03-17-2008, 09:39 PM -
Exception in thread "main" java.net.ConnectException: Connection timed out
By osval in forum Advanced JavaReplies: 1Last Post: 07-27-2007, 10:59 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM -
Problem with "GregorianCalendar"
By tola.ch2004 in forum New To JavaReplies: 2Last Post: 07-12-2007, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks