• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor's call

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

wht's the o/p of this?can any one explain the flow of pgm?



// file TestA.java

package a;

public class TestA {
protected int i = 1;
}// file TestB.java


package b;

import a.TestA;

public class TestB extends TestA {

static int i = 2;

public static void main(String[] args){
TestB testB = new TestB();
testB.test();
TestC testC = new TestC();
testC.test();
}

public void test(){
System.out.println("TestB i=" + i);
}

static class TestC{
static int i = 3;
public void test(){
System.out.println("TestC i=" + i);
}
}

}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is pretty straightforward.

The answer really doesn't depend on constructors.

In both TestB, TestC, and TestA, default constructors are created.

The method in TestB prints out 2, and the method in TestC prints out 3.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question tests the concept of "variable hiding and shadowing".
 
reply
    Bookmark Topic Watch Topic
  • New Topic