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

access modifier

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class poly{
public static void main(String a[]){
A ref1=new c();
B ref2=(B)ref1;
System.out.println(ref2.g());
}
}

class A{
private int f(){ return 0;}
public int g(){ return 3;}
}

class B extends A{
public int f(){ return 1;}
public int g(){ return f();}
}


class c extends B{
public int f(){ return 2;}

}

this program prints 2 but if i change the access modifier of f() in class B
to private it prints 1....whereas for anyother it prints 2...how is it being interpreted by compiler
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know,

when f() in B is coderanch, f() in C is interpreted as overriding f() in B.
so, the f() in C is called & 2 is output.

when f() in B is changed to private, f() in C is interpreted as "new method" (not overriding). so, the f() in B is called & 1 is output.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Gunj Agarwal,
The statement System.out.println(ref2.g()); calls the g() method in B class because ref2 is a reference of B and g() is not overriden in C. Now inside g() method we have the statement return f(); Now the call to f() can be considered as this.f() since this here denotes a class C object the java runtime checks if f() is overriden in C. Now since f() is overriden in C the method in C is called which returns 2 and 2 is printed.
Now if you make f() in B as private then the answer is 1 because private methods cannot be overriden. Hence the call return f(); in method g() of B will call the f() method in B itself. For anyother access modifiers of f() in B such as protected or default or public f() can be overriden.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when private access modifier is used -- overriding concept does not come into picture.This is bcoz private fields or methods are visible only inside the class where they are defined. so here in class c we are not overriding f() rather a new function is defined with same name i.e. f().

so during evaluation of the method (at run time only the f() in B class is called). is it clear?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic