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
17,012 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
0 votes

public class Main {
    public static void main(String[] args) {
      ParentClass pc = new ChildClass();
      pc.display();
  }
}

public class ParentClass{
  public static void display(){
    System.out.println("hello from parent static-display");
  }
  public void show(){
    System.out.println("hello from parent non-static-show");
  }
}
public class ChildClass extends ParentClass{
  public static void display(){
    System.out.println("hello from child static-display");
  }
  public void show(){
    System.out.println("hello from child non-static-show");
  }
}

Output:

hello from parent static-display
Explanation:
Here, we are creating an object for the parent class and referring to the child class which both has static method, since we cannot create object for static method, we are now creating an object and then pointing to child, so the parent class will hide the static method in the child class(method hiding).
Fun way to remember:
point no:1 creating an object for static method is wrong
point no:2 Okay, you created an object and referring to the child 
ParentClass pc = new ChildClass();
pc.display();
point no:3 Parent class will say, I should not have let you create the object in the first place yet, I allowed you to create but now you are going to kill(override)me. I can't bear it, I will block you(method hiding) 
answered Sep 2, 2022 by Tyro

edited Mar 5

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,374 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,245 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 877 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,397 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,217 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,104 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,741 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,283 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
992 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,615 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