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
69
votes
3 answers

Overriding a static method in python

Referring to the first answer about python's bound and unbound methods here, I have a question: class Test: def method_one(self): print "Called method_one" @staticmethod def method_two(): print "Called method_two" …
Emma
  • 52,713
  • 4
  • 19
  • 10
69
votes
5 answers

How do I override default PrimeFaces CSS with custom styles?

I want to change the size of a PrimeFaces component. For example, a . It has a class called ui-orderlist-list which is defined in primefaces.css with a fixed 200x200 dimension. No matter what I do in my theme.css, it is overwritten by…
Samuel Tian
  • 797
  • 1
  • 6
  • 8
69
votes
9 answers

Android Overriding onBackPressed()

Is it possible to override onBackPressed() for only one activity ? On back button click I want to call a dialog on a specific Activity, but in all other activities i want it to work as it worked before (going to previous activities). EDITED Thank…
Jilberta
  • 2,836
  • 5
  • 30
  • 44
68
votes
9 answers

Detect if a method was overridden using Reflection (C#)

Say I have a base class TestBase where I define a virtual method TestMe() class TestBase { public virtual bool TestMe() { } } Now I inherit this class: class Test1 : TestBase { public override bool TestMe() {} } Now, using Reflection, I…
Andrey
  • 20,487
  • 26
  • 108
  • 176
67
votes
3 answers

Overriding properties in python

So, I'm trying to figure out the best (most elegant with the least amount of code) way to allow overriding specific functions of a property (e.g., just the getter, just the setter, etc.) in python. I'm a fan of the following way of doing…
Jessica Hamrick
  • 773
  • 1
  • 5
  • 6
66
votes
5 answers

Why inner class can override private final method?

I wondered if it makes sense to declare a private method as final as well, and I thought it doesn't make sense. But I imagined there's an exclusive situation and wrote the code to figure it out: public class Boom { private void touchMe() { …
chicout
  • 937
  • 7
  • 16
66
votes
7 answers

Overriding public virtual functions with private functions in C++

Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public…
Ben Martin
  • 1,470
  • 2
  • 13
  • 16
66
votes
17 answers

'Shadows' vs. 'Overrides' in VB.NET

What is the significance of the two keywords Shadows and Overrides? What they do and for which context is one or the other preferable?
Jeff
  • 8,020
  • 34
  • 99
  • 157
65
votes
6 answers

Overriding Equals method in Structs

I've looked for overriding guidelines for structs, but all I can find is for classes. At first I thought I wouldn't have to check to see if the passed object was null, as structs are value types and can't be null. But now that I come to think of it,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
65
votes
2 answers

How do I override CSS set on a pseudo element?

I know that has been asked before, but I find no clean way of overriding this CSS: .ui-input-search:after { content: ""; height: 18px; left: 0.3125em; margin-top: -9px; opacity: 0.5; position: absolute; top: 50%; …
frequent
  • 27,643
  • 59
  • 181
  • 333
64
votes
2 answers

How to enforce the 'override' keyword?

Is there any way to enforce the usage of the C++11 override keyword in Visual C++ 2012? (i.e. if I forget to say override, then I want to get a warning/error.)
user541686
  • 205,094
  • 128
  • 528
  • 886
63
votes
3 answers

Overriding vs method hiding

I am a bit confused about overriding vs. hiding a method in C#. Practical uses of each would also be appreciated, as well as an explanation for when one would use each. I am confused about overriding - why do we override? What I have learnt so far…
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166
62
votes
8 answers

Do we really need @Override and so on when code Java?

Possible Duplicate: When do you use Java's @Override annotation and why? I wonder what the functionality of adding @Override in front of the code we would like to override is. I have done with and without it, and it seemed that everything was…
zfm
  • 1,906
  • 2
  • 17
  • 28
61
votes
5 answers

Overriding GetHashCode for mutable objects?

I've read about 10 different questions on when and how to override GetHashCode but there's still something I don't quite get. Most implementations of GetHashCode are based on the hash codes of the fields of the object, but it's been cited that the…
Davy8
  • 30,868
  • 25
  • 115
  • 173
61
votes
4 answers

Should I add an @Override annotation when implementing abstract methods in Java?

When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
Eyvind
  • 5,221
  • 5
  • 40
  • 59