Reputation: 9043
Was curious about the correct definition of the overridden method and the overriding method. In my understanding the overriding method is the method in the subclass that overrides the method with same signature and return type in the base class.
I have however seen sources refer to the overridden method as being the method in the subclass that overrides the method in the base class.
So which is the overridden method and which is the overriding method?
Sorry if this is a bit of a silly question
Upvotes: 1
Views: 130
Reputation: 15052
A direct example from the Java Documentation :
public class Animal {
public static void testClassMethod() {
System.out.println("The class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("The instance " + " method in Animal.");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method" + " in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
The reason I used this example is, look at the scenario from your real-world situation. A Animal
might have certain general features. But a Cat
will have some features that are different from a generic Animal
, but certain features that are an improvement over the generic Animal
features. So, the Cat
seems to override (will contain the overriding methods) the Animal
features.
Another simple example if you are interested in cars. Say, there is a Car
. It'll have an acceleration
method. But a Ferrari
will obviously have a better acceleration
than a Car
. But, a Ferrari
is a Car
. So, Ferrari
overrides a method in Car
. SO, overriding method is in subclass and the overriden method is in the base class.
So, do you get the point now? Overriding methods are present in the subclasses. But the methods that are overriden are present in the base class.
Upvotes: 2
Reputation: 328618
class A {
public void method() {
System.out.println("I don't know if I am overriden, but I'm not overriding anything");
}
}
class B extends A {
public void method() {
System.out.println("I am overriding A.method() which has now been overriden");
}
}
You can also read the section of the JLS that describes overriding, implementing, hiding, overloading etc. for more in depth understanding.
Upvotes: 3
Reputation: 114777
Let's assume we have a class SubClass extends SuperClass
, then:
SuperClass#method <- overridden
^
|
overrides
|
SubClass#method <- overriding
Apart from that, wikipedia tells us:
The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
Upvotes: 3