Are static methods inherited in Java?

Java 8Object Oriented ProgrammingProgramming

The static keyword is used to create methods that will exist independently of any instances created for the class.

Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.

We can inherit static methods in Java.

Example

In the example we are creating a class named Demo and, declared a static method named display().

We created another class Sample, extended the Demo class and tried to access the display() method using the sub class object.

Example

Live Demo

class Dem{
   public static void display(){}
}
public class Sample extends Dem {
   public static void display(){
      System.out.println("Hello this is a static method");
   }
   public static void main(String args[]) throws Exception{
      new Sample().display();
   }
}

Output

Hello this is a static method
raja
Published on 08-Jan-2018 12:10:37
Advertisements