• 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

overriding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when I compile this code through Java 5 version their come compilation error. pleas explain what is the reason for that.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m is a private method of A. You can't access it in B.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here we have got 2 cases -> Lets address one by one

Case (1) x.m();

This line absolutely ruled out. The code tries to access a private method outside its class. Strict NO!! NO!! to this.

Case (2) y.m();

Objects are created only during runtime. And Java compiler does n't know what Object the reference (Here "y") is referring to.
Hence, according to Java compiler, the code "y.m()" states that "y" is just the reference to the class "A"..hence the error (As per Case(1)'s rule)

Happy Learning!
 
Binesh Thusantha
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
private void m(){
System.out.println("A");
}
}
class B extends A{
private void m(){
System.out.println("B");
}
public static void main(String [] args){
A x = new A();
B y = new B();
x.m();
y.m();
}
}

If i change B y = new A(); as B y = new B(); but the compiler complain the error. so what please explain what is the reason.
Binesh
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line B y = new A() will result in error because u can add a derived class object to a base class reference but not a base class reference to a derived class. Here A is a base class and B is a derived class.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Binesh ,

Compilation error is coming for x.m() [ as m() has private access in A]. After your change, problem at y.m() is gone. If you remove x.m() it will run fine.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Binesh,

remember, you can not call private methods/read private attributes from outside of the class, wich defines the private methods/attributes!

You should read
JLS 6.6 for the access modfiers of java.

mfg,
Flom
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please confirm,calling the method m() with a parent type reference 'y' will not cause a compilation error.
as this was the output:
The method m() from the type A is not visible
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
calling the method m() with a parent type reference 'y' will cause a compilation error
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic