• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Inner class as a subclass of Outer class.

 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this small code to clear out my doubt. What I can't understand is though private method of outer class is accessible in the inner class, why can't it be overridden?

Output:
A
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidharth Khattri wrote:I wrote this small code to clear out my doubt. What I can't understand is though private method of outer class is accessible in the inner class, why can't it be overridden?


Output:
A


There is no inner class in this example. And I formatted the example for you -- notice how much easier is it to read?

Henry

 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm so sorry, the code was wrong. This is the correct one:


I made the correction on the OP. I don't know how to indent the code on posts. I use TAB/space, but it returns back to the left.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidharth Khattri wrote:
I made the correction on the OP. I don't know how to indent the code on posts. I use TAB/space, but it returns back to the left.


Spaces work fine in code tags -- please add the spaces back. Also, can you elaborate your question ?... not completely sure what you are asking.


Update: I tested your latest code. It works as expected.... so please explain what you expected when you ran the program.

Henry
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Sidharth Khattri wrote:
I made the correction on the OP. I don't know how to indent the code on posts. I use TAB/space, but it returns back to the left.


Spaces work fine in code tags -- please add the spaces back. Also, can you elaborate your question ?... not completely sure what you are asking.

Henry


If inner class B can access private method dostuff() of outer class,
eg: super.dostuff(); from inner class B will print A

why can't the same inner class override the private dostuff() of the outer class if this inner class already have access to outer class private method?
eg: A t = new B();
t.dostuff();
in Outer class t.dostuff(); doesn't use polymorphic override, and prints "A".
If it were a polymorphic override, the output would have been "B".

If it were for this code:

the compiler would have issued an error:
A.java:14: error: dostuff() has private access in A
t.dostuff();
^
1 error

because dostuff() has private access in A t.dostuff();
so B can't access it.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidharth Khattri wrote:
If inner class B can access private method dostuff() of outer class,
eg: super.dostuff(); from inner class B will print A

why can't the same inner class override the private dostuff() of the outer class if this inner class already have access to outer class private method?
eg: A t = new B();
t.dostuff();
in Outer class t.dostuff(); doesn't use polymorphic override, and prints "A".
If it were a polymorphic override, the output would have been "B".



Oh I see what you are asking... Well, simply, that is how it works. Maybe the Java designers messed up, but regardless...

First, there are rules for overriding. One of them is based on accessibility -- so private methods are not normally overridden. Second, there are rules on what method calls are virtual (in C++ speak), and private methods are not. Normally, these two rules work well together. In the case of inner classes, it does seem confusing. The method can be overridden, since it is accessible. However, if you use a reference type, whose method defines it as private, the method is not treated as virtual.

Henry
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Sidharth Khattri wrote:
If inner class B can access private method dostuff() of outer class,
eg: super.dostuff(); from inner class B will print A

why can't the same inner class override the private dostuff() of the outer class if this inner class already have access to outer class private method?
eg: A t = new B();
t.dostuff();
in Outer class t.dostuff(); doesn't use polymorphic override, and prints "A".
If it were a polymorphic override, the output would have been "B".



Oh I see what you are asking... Well, simply, that is how it works. Maybe the Java designers messed up, but regardless...

First, there are rules for overriding. One of them is based on accessibility -- so private methods are not normally overridden. Second, there are rules on what method calls are virtual (in C++ speak), and private methods are not. Normally, these two rules work well together. In the case of inner classes, it does seem confusing. The method can be overridden, since it is accessible. However, if you use a reference type, whose method defines it as private, the method is not treated as virtual.

Henry


So private methods can't be overridden during dynamic calls regardless of the accessibility in java. Because that is how it is.
Thanks Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic