• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Q from JQplus

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following classes and declarations, which of these statements are true?
class A
{
private int i = 10;
public void f( ) { }
public void g( ) { }
}
class B extends A
{
public int i = 20;
public void g( ) {}
}
A a = new A( );
A b = new B( );
A.System.out.println(b.i); will print out 20
B.System.out.println(b.i); will print out 10
C.None of above
the answer is C, but I donot understand why it is not 20.
[This message has been edited by Wesley Liu (edited May 21, 2001).]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Can you recompile your question with full code as I think in your answers section you have three A options. I assume that they are part of code, so wanted to be sure that you didn't missed anything else in code while putting down the question.
regards
Gaurav Mantro.
------------------
http://www.mantrotech.com
 
Wesley Liu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wesley Liu:
Given the following classes and declarations, which of these statements are true?
class A
{
private int i = 10;
public void f( ) { }
public void g( ) { }
}
class B extends A
{
public int i = 20;
public void g( ) {}
}
A a = new A( );
A b = new B( );
Options:
A.System.out.println(b.i); will print out 20
B.System.out.println(b.i); will print out 10
C.None of above
the answer is C, but I donot understand why it is not 20.
[This message has been edited by Wesley Liu (edited May 21, 2001).]


 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have some very basic doubts ...
===========================================================
class A
{
private int i = 10;
public void f( ) { }
public void g( ) { }
}
class B extends A
{
public int i = 20;
public void g( ) {}
}
A a = new A( );
A b = new B( );
Options:
A.System.out.println(b.i); will print out 20
B.System.out.println(b.i); will print out 10
=========================================================
Are the last two lines valid? Can you refer System.out.println() with a user defined reference type such as A or B as above?
-Thanks
 
SRV Gopal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry Guys ... that was stupid on my part. Really sorry about the earlier mail
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wesley Liu,
when you use a variable but a method, It is the compile-time type rather than the run-time type of the reference determines the version of the variable it uses. it involves no dynamic binding.
Therefore, in the code you provided, since b is declared type A, the A's version of i was used. It could be determined in the compile time.
Hope it helps
James
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B]Given the following classes and declarations, which of these statements are true?
class A
{
private int i = 10;
public void f( ) { }
public void g( ) { }
}
class B extends A
{
public int i = 20;
public void g( ) {}
}
A a = new A( );
A b = new B( );
A.System.out.println(b.i); will print out 20
B.System.out.println(b.i); will print out 10
C.None of above
the answer is C, but I donot understand why it is not 20.

(b.i) --- here the reference type is A and object is of type B
now the variables of the reference type are picked up and the methods of object type.
now when u say b.i , the control is transferred to the class A to fetch the value of i.
but i is declared private in class A and so it is not accessible outside A.
if u declare i as public in A, the answer will be 10.
hope it is clear now
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I agree with Sona Nagee.
When you create an object by a reference of the super class,
the method calls are decided by the type of the object , in this case, B Class's method, overrides the A class's method. Whereas, in case of variables, they are hidden and the variables are refferred by the type of reference in the problem in context it's A Class's variable. But, the real trick in the question
is the accessbility modifier of the variable "i" in the Base class A (its private), so you can inherrit it , nor can you access it outside the class A. So the answer is (C), it should be COMPILER ERROR, "Variable i class A not accessible in class B" .
Hope the issue is clear now.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 22, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic