• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Parikosh 6th Question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer for the question is B
Could someone explain the same. Thanks a lot.
=================================================================
class B{
int x;
B(){ x = 10;}
void m1(){x = 20;}
void m1(int x){this.x = x;}
}
class A extends B{
int x;
A(){x = 20;}
void m1(int x){super.x = x;}
public static void main(String arf[]){
B x = new A();
System.out.println(x.x);
x.m1();
System.out.println(x.x);
x.m1(30);
System.out.println(x.x);
((A)x).m1(30);
System.out.println(x.x);
}
}
a. 10 20 20 20
b. 10 20 30 30
c. 20 20 20 30
d. 20 20 20 20
e. Compiler error or run time error.
f. None of the above, it will give another output.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since x is reference of type B(base class), x.x is always refer to the Base integer member x, not the hiding integer member of A.
But when it comes to methods, actual object method would be invoked. That means if A has a over-riding method, that is the one is invoked. If there is none of over-riding method, then the base class method is invoked.
Bala Arul.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are creating a reference of super class B which an object of the subclass A. In such a case, remember that methods will be called based on the object type(ie that of A) while instance variables are resolved using the reference(that of super class B). Keeping this in mind, lets see what happens :-
1.First we have B x = new A(); and the subsequent printing of the instance variable, which will give us x from the super class
ie 10.
2.Next you call x.m1(). As there is no overridden method in the subclass A which matches this signature, we call the method m1() from superclass B, which changes x to 20. This is printed out now.
3.Calling x.m1(30) invokes the overridden method for subclass A, which accesses the super class's hidden variable(since A also has an instance variable x) and changes it to 30. This is printed out when we say x.x as the instance variable from
super class B is always being printed out.
4. This is nothing but step 3 all over again. Can you now see why? So 30 is printed out once more.
Hope this gives you an understanding.
Regards
Sajida
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sc,

The declared type 'B' is used to access variables. Value of 'x' in class 'B' is '10'.

There is no method 'm1()' in the actual type 'A' so the superclass is searched. 'm1()' in 'A' set 'x' to '20'. 'x.x' again uses the declared type 'B' to access the instance variable 'x' and '20' is output.

The method 'm1(int x)' is declared in the actual type 'A' so it is used. The method itself sets 'super.x' or 'B.x' to the value of the parameter variable 'x', '30'. Again, 'x.x' in the print refers to the declared type of class 'B'. The value of 'x' in 'B' is '30'.

The cast '((A)x)' is redundant ... 'x' has an actual type of 'A' and as mentioned above, the method 'm1(int x)' is declared in 'A' and references the superclass variable 'x'; so output is again '30'.
See JLS §15.11 Field Access Expressions and JLS §15.12 Method Invocation Expressions
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic