aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Inner class as a subclass of Outer class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Inner class as a subclass of Outer class." Watch "Doubt in Inner class as a subclass of Outer class." New topic
Author

Doubt in Inner class as a subclass of Outer class.

Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

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


OCPJP 6 - 96%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Sep 16, 2013
Posts: 121

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Sep 16, 2013
Posts: 121

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Inner class as a subclass of Outer class.
 
Similar Threads
Doubt in innerclass
inner class doubt
Doubts regarding Method Local Inner Class
Nested classes - Series of doubts
inner class doubt