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

Force child class to override parent's methods

Suppose I have a base class with unimplemented methods as follows: class Polygon(): def __init__(self): pass def perimeter(self): pass def area(self): pass Now, let's say one of my colleagues uses the Polygon…
Aditya Barve
  • 1,351
  • 1
  • 9
  • 13
84
votes
7 answers

Why do we need the new keyword and why is the default behavior to hide and not override?

I was looking at this blog post and had following questions: Why do we need the new keyword, is it just to specify that a base class method is being hidden. I mean, why do we need it? If we don't use the override keyword, aren't we hiding the base…
Sandbox
  • 7,910
  • 11
  • 53
  • 67
83
votes
4 answers

The difference between virtual, override, new and sealed override

I'm pretty confused between some concepts of OOP: virtual, override, new and sealed override. Can anyone explain the differences? I am pretty clear that if the derived class method is to be used, one can use the override keyword so that the base…
xorpower
  • 17,975
  • 51
  • 129
  • 180
80
votes
9 answers

C# optional parameters on overridden methods

Seems like in .NET Framework there is an issue with optional parameters when you override the method. The output of the code below is: "bbb" "aaa" . But the output I'm expecting is: "bbb" "bbb" .Is there a solution for this. I know it can be solved…
SARI
  • 854
  • 1
  • 8
  • 12
80
votes
4 answers

How do Java method annotations work in conjunction with method overriding?

I have a parent class Parent and a child class Child, defined thus: class Parent { @MyAnnotation("hello") void foo() { // implementation irrelevant } } class Child extends Parent { @Override foo() { //…
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
79
votes
9 answers

Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'

Xcode 6.3. Within a class implementing UITextFieldDelegate protocol, I would like to override touchesBegan() method to possibly hide the keyboard. If I avoid a compiler error in the function spec, then there is a complier error trying to read the…
SoCor
  • 803
  • 1
  • 7
  • 6
78
votes
9 answers

Override a function call in C

I want to override certain function calls to various APIs for the sake of logging the calls, but I also might want to manipulate data before it is sent to the actual function. For example, say I use a function called getObjectName thousands of times…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
78
votes
6 answers

is it possible to call overridden method from parent struct in Golang?

I want to implement such code, where B inherit from A and only override A's Foo() method, and I hope the code to print B.Foo(), but it still print A.Foo(), it seems that the receiver in Golang can't work like this in C++, in which when dynamic…
zhaozhi
  • 1,491
  • 1
  • 16
  • 19
77
votes
13 answers

Overriding member variables in Java ( Variable Hiding)

I am studying overriding member functions in Java and thought about experimenting with overriding member variables. So, I defined classes public class A{ public int intVal = 1; public void identifyClass() { System.out.println("I…
Kalyan Raghu
  • 1,005
  • 1
  • 10
  • 19
75
votes
10 answers

Overriding equals() & hashCode() in sub classes ... considering super fields

Is there a specific rule on how Overriding equals() & hashCode() in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ... For instance, Netbeans generated equals() &…
wj.
  • 2,001
  • 3
  • 18
  • 19
74
votes
3 answers

What does @Override mean?

public class NaiveAlien extends Alien { @Override public void harvest(){} } I was trying to understand my friend's code, and I do not get the syntax, @Override in the code. What does that do and why do we need in coding? Thanks.
Woong-Sup Jung
  • 2,337
  • 4
  • 21
  • 20
74
votes
5 answers

Does final imply override?

As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found. My understanding of the final keyword is that it tells the compiler…
quant
  • 21,507
  • 32
  • 115
  • 211
73
votes
2 answers

Override identifier after destructor in C++11

Does the override identifier after virtual destructor declaration have any special meaning? class Base { public: virtual ~Base() {} virtual int Method() const {} }; class Derived : public Base { public: virtual ~Derived()…
71
votes
7 answers

Android emulator camera custom image

Does anybody know is it possible to open some personal picture when emulator camera starts? I have an application which is based on image analysis and I would like when I click a button for camera that camera opens not that default Android emulator…
Cristiano
  • 3,099
  • 10
  • 45
  • 67
70
votes
4 answers

Android @Override usage

(Newbie to Java, old time C# guy.) I have noticed a lot of the use of @Override in Android example code. I thought that all Java methods were by default "Virtual"? What then does @Override do? Example: private class HelloWebViewClient extends…
Ian Vink
  • 66,960
  • 104
  • 341
  • 555