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
16,530 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,268 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
2,091 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,360 points

edited Oct 19, 2023 by anonymous 736 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,920 points
4,135 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
2,115 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
1,015 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,920 points
4,551 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,140 points
4,129 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
917 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,535 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