• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubts ... need help

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 ]
a call super() is made by constructor of every subclass implicitely but is this true for methods also.(i. e) if a call is made to the method of subclass does it check for that method in the super class also.
the following code results in compile time error saying"method getFields() not found in class Base"
please explain
class Base {

}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
System.out.println(a.getFields());
}
}
2]why does this give me ans as 10,0 & 20
10,0 i can understand but if the valiue v.i has been made 10 then why does it give20 again.
please clarify.
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);

}
//End of another
}
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. the output gives an error because if you need to access a method defined in a subclass through a super class reference , you need to have that method defined in the superclass too, only then the effect of overriding or overloading(as you might have intended to do) will be achieved.. otherwise not.
2. This program deals with the pass-by-value characteristic of Java.
A copy of the reference is passed as the parameter which means that though we can't change the reference , we can change the contents of the type of object being pointed to by the reference.
The program prints the value 10 after this line :
System.out.println(v.i+ " "+i);
because v has been assigned a new reference (vh) (changing a reference works as long as it is within a method) and so it prints 10 (the value of v.i is initialized to 10) and the value of local variable i is 0 .. hence the output 10 0.
now as for the value 20 being printed, since the value of the member variable i of v object was altered by the line v.i=20 within the method, it will get changed (refer to the second para) and will print 20.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shilpa!
the confusing thing is the method another(valhold v,int i),it has used the same name v ,which is different from original v.Let us take it as v1.Now when we call this method,v1 is also pointing to the i of v ,when we set v1.i=20(originally v.i in the question),the i of main v also gets value 20.Then the value of v1 is pointed to vh,which has nothing to do with original v whose value is set to 20.Hence it gives twenty.
hope u get it.Any doubts u can mail me at puneetpruthi@usa.net
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic