File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
Binesh Thusantha
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m is a private method of A. You can't access it in B.
 
Balaji VR
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sreeram Desigan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
tapan saha
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Flom Xanther
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Khaled Abdulraouf
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calling the method m() with a parent type reference 'y' will cause a compilation error
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic