Questions tagged [overriding]

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.

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.

Don't confuse it with

Read on Wikipedia

Example in Java

class Thought {
    public void message() {
        System.out.println("I feel like I am diagonally parked in a parallel universe.");
    }
}

public class Advice extends Thought {
    @Override  // @Override annotation in Java 5 is optional but helpful.
    public void message() {
        System.out.println("Warning: Dates in calendar are closer than they appear.");
    }
}
8096 questions
60
votes
4 answers

Is there any point in using `override` when overriding a pure virtual function?

For example: class Base { virtual void my_function() = 0; }; class Derived : Base { void my_function() override; }; From what I read, the override keyword is used to make sure that we have the correct signature in the function that we are…
R2B2
  • 1,541
  • 1
  • 12
  • 19
59
votes
7 answers

What is the use of 'abstract override' in C#?

Just out of curiosity I tried overriding a abstract method in base class, and method the implementation abstract. As below: public abstract class FirstAbstract { public abstract void SomeMethod(); } public abstract class SecondAbstract :…
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
59
votes
4 answers

What is @Override for in Java?

Possible Duplicate: When do you use Java’s @Override annotation and why? Is there any reason to annotate a method with @Override other than to have the compiler check that the superclass has that method?
user65374
  • 24,139
  • 4
  • 19
  • 7
58
votes
13 answers

Is there a way to make a method which is not abstract but must be overridden?

Is there any way of forcing child classes to override a non-abstract method of super class? I need to be able to create instances of parent class, but if a class extends this class, it must give its own definition of some methods.
user517491
58
votes
12 answers

How to call the overridden method of a superclass?

How can I call the eat and drink method of the Animal class with the myAnimal instance in the code? public class Animal { public void eat() { System.out.println("Animal Eats"); } public void drink() { …
Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79
57
votes
6 answers

How to overload constructor of an Object in JS (Javascript)?

Can I do something like?: function User(form) { this._username = form.username.value; this._password = form.password.value; this._surname = form.surname.value; this._lastname = form.lastname.value; this._birthdate =…
orshachar
  • 4,837
  • 14
  • 45
  • 68
57
votes
12 answers

Force a class to override the .equals method

I have a bunch of class who implement a common interface : Command. And this bunch of class goes to a Map. To get the Map working correctly, I need to each class who implements Command to override the Object.equals(Object other) method. it's…
Antoine Claval
  • 4,923
  • 7
  • 40
  • 68
57
votes
3 answers

Can a static method be overridden in C#?

I was told that static methods are implicitly final and therefore can't be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just class methods, what is the real use of having them?
aspiring
  • 1,557
  • 2
  • 20
  • 43
56
votes
3 answers

Overriding a Base's Overloaded Function in C++

Possible Duplicate: C++ overload resolution I ran into a problem where after my class overrode a function of its base class, all of the overloaded versions of the functions were then hidden. Is this by design or am I just doing something…
Greg
  • 2,229
  • 1
  • 16
  • 20
55
votes
8 answers

How to call a superclass method using Java reflection

I have two classes: public class A { public Object method() {...} } public class B extends A { @Override public Object method() {...} } I have an instance of B. How do I call A.method() from b? Basically, the same effect as calling…
Ted
  • 551
  • 1
  • 4
  • 3
54
votes
4 answers

How to override method via keyboard shortcut in Android Studio

I can finally ask a question to get my points up. In android studio, I want to be able to override the method, however I do not know the keyboard shortcut. Does anyone know?
ahsan.dev
  • 689
  • 1
  • 5
  • 10
54
votes
2 answers

Overriding synchronized methods in Java

Let's say I have a synchronized method on some class: abstract class Foo { public synchronized void foo() { // synchronized! // ... }; } and I overrode it without using the synchronized modifier: class Bar extends Foo { …
Markus A.
  • 12,349
  • 8
  • 52
  • 116
53
votes
2 answers

Python: multiplication override

So, I've got a custom class that has a __mul__ function which works with ints. However, in my program (in libraries), it's getting called the other way around, i.e., 2 * x where x is of my class. Is there a way I can have it use my __mul__ function…
Colin DeClue
  • 2,194
  • 3
  • 26
  • 47
53
votes
13 answers

Override Default Constructor of Partial Class with Another Partial Class

I don't think this is possible, but if is then I need it :) I have a auto-generated proxy file from the wsdl.exe command line tool by Visual Studio 2008. The proxy output is partial classes. I want to override the default constructor that is…
Elijah Manor
  • 17,923
  • 17
  • 72
  • 79
52
votes
4 answers

Requiring virtual function overrides to use override keyword

C++11 added override to ensure that member functions you write that you intend to override base-class virtual functions actually do (or won't compile). But in a large object hierarchy, sometimes you could accidentally end up writing a member…
Barry
  • 286,269
  • 29
  • 621
  • 977