View Single Post
  #2 (permalink)  
Old 07-20-2007, 07:43 AM
cruxblack cruxblack is offline
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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
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();
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 this
Code:
public Testing1//the Testing1 default constructor { Scanner scan = new Scanner(System.in); int i = scan.nextInt();//it'll ask for an input }
Testing3 are inherited from Testing2 which were inherited from Testing1
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

If i may ask, what was the code supposed to do actually?
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?
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
Reply With Quote