top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we override private methods in Java if yes explain how?

+3 votes
544 views
Can we override private methods in Java if yes explain how?
posted Feb 10, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
No I don't think so.
then what it the use of the private specifiers.

1 Answer

+1 vote

yes we can access it see this example and read comments for more clarification:

    class A
    {
    private void methodT()
    {
    System.out.println("I am in Class A");
    }
    class B extends A
    {
    public void methodT()  
    {
    System.out.println("Hey I am overridden ");
    }
  }
  // check this output  
   public static void main(String args[])
    {
    new A().new B().methodT();
    }
 }

we modified class A private method in class B.

answer Mar 22, 2016 by Shivam Kumar Pandey
...