• Register
1 vote
21 views

Problem:

Hello all,
I just started learning java OOP. Things are getting clear to me gradually. My question is very simple. Can we override a private method in java? Please reply to this thread with a legit explanation.

Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

In simple words, NO! You can not override a private method in a java program. Overriding or Polymorphism is not possible for a private class. Please, look into the program below to understand things well.

class Animal{ 
  private void puppy(){ 
     System.out.println("German Shephard");      
  } 
} 
    
class Derived extends Animal{ 
  private void puppy(){ 
     System.out.println("Derived puppy");   
  } 
  public static void main(String[] args){ 
      Animal myobj = new Derived(); 
      myobj.puppy(); 
  }   
}

If you run the program. It will throw back a compiler error. The reason behind it is the function puppy() has private access inside Animal class. Here the compile tries to call the Animal class not the Derived class. 

13 9 6
94,240 points

Related questions

1 vote
1 answer 11 views
11 views
Problem: Hello Kodlogs, I am a Java developer and recently trying to adopt a new programming language. Therefore, I took up the Python programming language and started writing codes using it. I was distinguishing the python methods and functions. As far as I come to know ... and how do you implement it in a python program? I am looking forward to having your answers. Thanks for your valuable time.
asked Jul 7, 2020 adamSw 11.3k points
0 votes
1 answer 9 views
9 views
Is this because the main function is in the same class as the method don, or because of overriding?
asked Feb 9 TeamScript 13.5k points
0 votes
1 answer 6 views
0 votes
1 answer 9 views
9 views
Problem: What is private member function in C++? How do you use private members in C++? What is member function with example? What is data member in C++ with example? How can i declare a function/class as friend of that particular class, and then the member will be accessible inside that function or class object without access specifier check?
asked Dec 23, 2020 Mashhoodch 9.1k points
0 votes
1 answer 49 views
49 views
Problem: I am working with classes in C++ and trying to access a function that is private but continuously failing to do so. How can we access private member functions?
asked Oct 13, 2020 Code Learner 5.8k points
0 votes
1 answer 12 views
12 views
Problem: somehow I still get error down there I described error, any help would be appreciated. I am not really expert with import of this and especially API itself Hello this is my code and when I try to compile it I get error saying: Note: MyClass.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
asked Feb 20 Ali 4.4k points
0 votes
1 answer 14 views
14 views
What is the difference between method overloading and method overriding explain with examples?
asked Sep 28, 2020 Sofi55 1.1k points
0 votes
1 answer 4 views
4 views
Problem: In Java Programming, Can we call a static strategy for a theoretical class? Indeed I realize we can't utilize static with a technique for a theoretical class. in any case, I need to know why?
asked Feb 19 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: can you override static method in java
asked 3 days ago Hira 2.2k points
0 votes
1 answer 15 views
15 views asked Jan 24 ahmedadel9090 2.6k points