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.");
    }
}
7556 questions
1542
votes
15 answers

Why is it important to override GetHashCode when Equals method is overridden?

Given the following class public class Foo { public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as Foo; if (fooItem == null) { …
David Basarab
  • 67,994
  • 42
  • 125
  • 155
1278
votes
13 answers

'Must Override a Superclass Method' Errors after importing a project into Eclipse

Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error: The method must override a superclass…
Tim H
  • 13,263
  • 3
  • 15
  • 9
617
votes
11 answers

What issues should be considered when overriding equals and hashCode in Java?

What issues / pitfalls must be considered when overriding equals and hashCode?
Matt Sheppard
  • 111,039
  • 46
  • 105
  • 128
554
votes
22 answers

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods? If possible, please use an example.
sgokhales
  • 49,762
  • 33
  • 123
  • 158
384
votes
7 answers

What's wrong with overridable method calls in constructors?

I have a Wicket page class that sets the page title depending on the result of an abstract method. public abstract class BasicPage extends WebPage { public BasicPage() { add(new Label("title", getTitle())); } protected abstract…
deamon
  • 78,414
  • 98
  • 279
  • 415
361
votes
21 answers

Polymorphism vs Overriding vs Overloading

In terms of Java, when someone asks: what is polymorphism? Would overloading or overriding be an acceptable answer? I think there is a bit more to it than that. IF you had a abstract base class that defined a method with no implementation, and…
Brian G
  • 49,145
  • 57
  • 120
  • 139
358
votes
8 answers

Can I call a base class's virtual function if I'm overriding it?

Say I have classes Foo and Bar set up like this: class Foo { public: int x; virtual void printStuff() { std::cout << x << std::endl; } }; class Bar : public Foo { public: int y; void printStuff() { // I…
Alex
  • 13,729
  • 13
  • 55
  • 88
256
votes
7 answers

Implementing two interfaces in a class with same method. Which interface method is overridden?

Two interfaces with same method names and signatures. But implemented by a single class then how the compiler will identify the which method is for which interface? Ex: interface A{ int f(); } interface B{ int f(); } class Test implements A,…
Jothi
  • 13,342
  • 20
  • 59
  • 92
256
votes
10 answers

Override back button to act like home button

On pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state. In the Android docs it states: ...not all activities have the behavior that they are destroyed when BACK is pressed. When the user…
bdls
  • 4,548
  • 6
  • 22
  • 30
250
votes
14 answers

Calling virtual functions inside constructors

Suppose I have two C++ classes: class A { public: A() { fn(); } virtual void fn() { _n = 1; } int getn() { return _n; } protected: int _n; }; class B : public A { public: B() : A() {} virtual void fn() { _n = 2; } }; If I write the…
David Coufal
  • 5,063
  • 4
  • 25
  • 30
247
votes
5 answers

Is the 'override' keyword just a check for a overridden virtual method?

As far as I understand, the introduction of override keyword in C++11 is nothing more than a check to make sure that the function being implemented is the overrideing of a virtual function in the base class. Is that it?
aiao
  • 4,261
  • 3
  • 20
  • 43
241
votes
4 answers

What is the 'override' keyword in C++ used for?

I am a beginner in C++. I have come across override keyword used in the header file that I am working on. May I know, what is real use of override, perhaps with an example would be easy to understand.
SKPS
  • 4,404
  • 3
  • 21
  • 50
240
votes
9 answers

C++ "virtual" keyword for functions in derived classes. Is it necessary?

With the struct definition given below... struct A { virtual void hello() = 0; }; Approach #1: struct B : public A { virtual void hello() { ... } }; Approach #2: struct B : public A { void hello() { ... } }; Is there any difference…
Anarki
  • 4,123
  • 4
  • 17
  • 9
229
votes
4 answers

Why does an overridden function in the derived class hide other overloads of the base class?

Consider the code : #include class Base { public: virtual void gogo(int a){ printf(" Base :: gogo (int) \n"); }; virtual void gogo(int* a){ printf(" Base :: gogo (int*) \n"); }; }; class Derived :…
Aman Aggarwal
  • 3,557
  • 4
  • 22
  • 37
229
votes
14 answers

Difference between new and override

Wondering what the difference is between the following: Case 1: Base Class public void DoIt(); Case 1: Inherited class public new void DoIt(); Case 2: Base Class public virtual void DoIt(); Case 2: Inherited class public override void…
Shiraz Bhaiji
  • 60,773
  • 31
  • 133
  • 239
1
2 3
99 100