I'm very new to Java, and I just have a few questions that I can't seem to clear up even with some research.

What does it mean to override a method?

Also what is the difference between a static and non-static method?

Dani AI

Generated

A short, practical summary that fills the gap left by the replies from and .

Overriding: a subclass supplies its own implementation of an instance method declared in a superclass using the same method signature. At runtime the JVM dispatches calls to the actual object's class, so a reference typed as the superclass can execute the subclass version (this is the core of polymorphism). Overriding is different from overloading: overloading uses the same name with different parameter lists and is resolved at compile time.

Key rules and gotchas:

  • The name and parameter types must match exactly; return type must be the same or a covariant subtype.
  • Visibility cannot be reduced (a protected method in the superclass cannot be made private in the subclass).
  • The overriding method cannot declare new checked exceptions that the superclass method did not declare; unchecked exceptions are allowed.
  • Final methods (and private methods) are not overridable; static methods are not overridden but hidden. Constructors also cannot be overridden.
  • Use the @Override annotation — it catches accidental signature mismatches at compile time.

Example of overriding and runtime dispatch:

class Animal {
    String speak() { return "some sound"; }
}
class Dog extends Animal {
    @Override
    public String speak() { return "woof"; }
}
public class Demo {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a.speak()); // prints "woof"
    }
}

Static vs. non‑static (expanding on ): static methods belong to the class and are resolved at compile time. Declaring a static method with the same signature in a subclass hides the superclass method — it does not participate in polymorphism. Use static methods via the class name (ClassName.method()) to avoid confusion. Final practical tip: prefer @Override and check access/exception rules when a subclass method "doesn’t seem to override" — most problems come from small signature or modifier mismatches.

Recommended Answers

All 2 Replies

Also what is the difference between a static and non-static method?

as to this question: a non-static method can only be called through an instance of a class, for instance:

public class StaticNonStatic{
// first a non-static method
public void runNonStatic(String output){
System.out.println(output);
}
// second, a static method
public static void runStatic(String output){
System.out.println(output);
}
}

The next class here, is a short main method that shows how to use these methods:

public class TestClass{
public static void main(String[] args){
// to use a static method, you access that method 
// through the class:

StaticNonStatic.runStatic("Print this through a static method");

// now, to run a non-static method, you'll need an instance of 
// that class, so, first you'll have to instantiate an Object of the type
// StaticNonStatic

StaticNonStatic statNStat = new StaticNonStatic(); 
// using the default constructor
// now you can call the non-static method through this instance
statNStat.runNonStatic("Print this through a non static method");
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.