Can We Override Private Method in Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Can we override private method in Java?

Answer : No, we cannot override private methods in Java as if we declare any


variable ,method as private that variable or method will be visible for that class
only and also if we declare any method as private than they are bonded with class
at compile time not in run time so we cant reference those method using any
object so we cannot override private method in Java.

****************************************************************

Can you override private or static method in Java ?

As I said method overriding is a good topic to ask trick questions in


Java. Anyway, you can not override private or static method in Java, if you create
similar method with same return type and same method arguments in child class
then it will hide the super class method, this is known as method hiding. Similarly
you cannot override private method in sub class because it's not accessible
there, what you do is create another private method with same name in child
class. See Can you override private method in Java or more details.

You might also like