Overriding private or static method in Java

0 votes

How can I override a private or static method in Java?

Jul 31, 2018 in Java by misc.edu04
• 1,450 points
14,440 views

1 answer to this question.

0 votes

You cannot override a private or static method in Java. If you create a 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 a private method in sub class because it’s not accessible there. What you can do is create another private method with the same name in the child class. Let’s take a look at the example below to understand it better.




class Base {

private static void display() {

System.out.println("Static or class method from Base");

}

public void print() {

System.out.println("Non-static or instance method from Base");

}

class Derived extends Base {

private static void display() {

System.out.println("Static or class method from Derived");

}

public void print() {

System.out.println("Non-static or instance method from Derived");

}

public class test {

public static void main(String args[])

{

Base obj= new Derived();

obj1.display();

obj1.print();

}

}
answered Jul 31, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
1 answer

Non-static method within Static method in Java

As per my knowledge, you are getting this error ...READ MORE

answered Sep 28, 2018 in Java by anto.trigg4
• 3,440 points
1,021 views
0 votes
2 answers

Why it is not possible to define a static method in a Java interface?

Interfaces are concerned with polymorphism which is ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 59,230 points
1,522 views
0 votes
1 answer

Why main method is static in Java?

In Java, the `main` method is required ...READ MORE

answered Oct 19, 2023 in Java by anonymous
• 3,320 points

edited Oct 19, 2023 by anonymous 252 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,515 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
1,874 views
0 votes
2 answers

When to use static methods

Java main() method is always static, so that compiler ...READ MORE

answered Dec 29, 2020 in Java by Reshma
721 views
0 votes
2 answers

“Could not find or load main class” mean?

Use the final modifier to enforce good initialization. Avoid returning ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,910 points
4,130 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,120 points
3,743 views
0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,500 points
735 views
0 votes
1 answer

System.exit() method in Java

System.exit() is a method of System class ...READ MORE

answered Sep 28, 2018 in Java by code.reaper12
• 3,500 points
1,263 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP