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?
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?
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:
@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.
Jump to Post— moutanna 2Check on this link:
http://java.sun.com/docs/books/tutorial/java/IandI/override.htmlhope it helps.
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");
}
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.