Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can private methods be overridden?

 
priya thomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question from a mock exam:

Can a private method be overridden by a protected method:

Options1: True, Option 2: False

The correct answer as per the exam is Option1.

I am assuming the reason is: private methods CAN be overridden by protected methods, given that both methods are in the same class. Is that correct?

If it is a case of super class and sub class and the super class has the private method and the subclass has the same method with protected (or any) modifier, then the code will compile but we cannot say the method is overridden. Am i right?

Thanks
Priya
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to figure it out is to try it yourself...



In this code, the method of the subclass is *not* called, hence, technically, it hasn't been overridden.

Henry
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priya,

private methods can be overriden !

Here is a way:
Which class can access the private members of a class? The class itself AND any of its inner classes !

So if there is an inner class within a class, and that inner class is the sub class of the outer class and if the outer class has a private method, then the inner class can override that method and broaden its access within the inner class !

Hope you got my point.

-Reuel
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi priya,

It might seems that you can override private methods, but what is really happening is not overridden. Remember that private methods are not inherited, therefore you are just writing a method in the subclass that happen to have the same name and signature of its super type. The reason that you can get away with your code, is that when you call the b.method(), is that there is private method in your current class( therefore you can access it) at compile time. Also, I if the two classes are in one file, I don't think your code will compile, because only one class can have the "public" access modifier.

Regards,
Armando
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this code, Inner inherits (??) the Outer's printMe method. Compiling and running the above method as is we get the output:


But if Inner does not extend Outer then super.printMe() doesn't work.
If we comment out lines 7-9 and line 12, we get the output
This is because every private member of the Outer is visible to Inner.

In a sense, when the Inner class extends the Outer and redefines Outer's private methods I assume it is overriding Outer's methods though they are private (??).

-Reuel
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey marco!
You are 100% right, and I really like your example about inheritance and inner classes. In this case , still is not overridden. Your example is more about a shadow method. Since you didn't inherited printMe from Outer, you will have no issue in right your own printMe in Inner, but because is an non-static inner class member, you still have access to the outer private methods. Your example is very good and tricky, but once again , is shadow method and not overridden.

-Armando
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is not overridden at all. You are assigning Inner() object to Inner reference type (overridden comes to picture when reference type and object type are different).

one thing that is possible in inner class and not in inherited classes ( in context to private methods) is accessing private method with super keyword
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco your example is good. But as everyone says, still there is no overriding. Just to test it try this program



I've made the printMe method final in Outer class still you can declare printMe in the inner class...
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit,

I liked your explanation. That's right a private method can never be overriden.

But only thing that makes it "pseudo-possible" is the ability to call super.somePrivateMethodInSuperClass() in a sub-class. Which looks like calling a super class' version of the overriden method. I agree though it looks like overriding it is just shadowing.

I was tending to think in the way the question was asked in the mock exam Priya mentioned. May be the question should have been asked differently. It is misleading..


Reuel.
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:

This is not overridden at all. You are assigning Inner() object to Inner reference type (overridden comes to picture when reference type and object type are different).


testMe() is not the overriden method.


one thing that is possible in inner class and not in inherited classes ( in context to private methods) is accessing private method with super keyword


No the inner class (which does not extend the outer class) cannot access the outer class' private methods using super keyword. Because if inner doesn't extend anything super would refer to Object.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no overriding.


The code above prints "I am Outer"

While there is no overriding, this is an interesting example, good job Marco.

The way I would explain this is to say that an inner class can access a private method of the enclosing class it extends, but the method doesn't become a part of itself. So when a method of the same name is declared in the inner class, it doesn't override but merely hides the method defined in the enclosing superclass (however, the method in the enclosing superclass should still be accessible using the enclosing instance (in this case Outer.this.)
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


testMe() is not the overriden method.


I meant this line as both reference type and object type are same and no matter wat method called , its instance of Inner and type of Inner.



No the inner class (which does not extend the outer class) cannot access the outer class' private methods using super keyword. Because if inner doesn't extend anything super would refer to Object.


Yup agree.
 
Marvin Soliven
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Soto wrote:Yes, there is no overriding.


and if we comment out the printMe() method from the Inner class, we can see that private methods cannot even be inherited, i.e., calling inn.printMe() instead of inn.testMe() in the main() method, will generate a compile error.

hence, there will be nothing to override
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marvin Soliven wrote:
Ruben Soto wrote:Yes, there is no overriding.


and if we comment out the printMe() method from the Inner class, we can see that private methods cannot even be inherited, i.e., calling inn.printMe() instead of inn.testMe() in the main() method, will generate a compile error.

hence, there will be nothing to override

You are absolutely correct, Marvin. The outer class private methods (which are not inherited in Inner) are only accessible within Inner's code via either unqualified access or access through the Outer.this instance.

Welcome to the ranch, by the way.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man...
it's very easy to say when it is overridden and when it's not.
just use eclipse! lol

just kidding. but sure eclipse and other ides can help. xD
 
Greg Messner
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple way to verify that the Inner.printMe() is not overriding Outer.printMe() is to use the @Override annotation on Inner.printMe(), doing so will cause the compile to fail with the following message:

Outer.java:6: method does not override or implement a method from a supertype


If you leave off the @Override annotation it compiles just fine.
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private methods never can be overriden
 
Greg Messner
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna Srinivasan wrote:Private methods never can be overriden

Most here know that private methods cannot be overridden, I was just demonstrating that fact by using the @Override annotation.

If you read above some seem to be confused by using an inner class and what was happening when that inner class was a sub class of the outer class which contained the private method.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Messner wrote: I was just demonstrating that fact by using the @Override annotation.


Yup, that's a good approach, why didn't I think of it ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic