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
110
votes
4 answers

Is there any way in C# to override a class method with an extension method?

There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { …
Phred Menyhert
  • 2,420
  • 4
  • 19
  • 19
106
votes
17 answers

Why / when would it be appropriate to override ToString?

I'm studying C# and I wonder what the point and benefit of overriding ToString might be, as shown in the example below. Could this be done in some simpler way, using a common method without the override? public string GetToStringItemsHeadings { …
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
105
votes
10 answers

Safely override C++ virtual functions

I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I…
sth
  • 222,467
  • 53
  • 283
  • 367
104
votes
19 answers

Can I override and overload static methods in Java?

I'd like to know: Why can't static methods be overridden in Java? Can static methods be overloaded in Java?
giri
  • 26,773
  • 63
  • 143
  • 176
102
votes
7 answers

JavaScript override methods

Let's say you have the below code: function A() { function modify() { x = 300; y = 400; } var c = new C(); } function B() { function modify(){ x = 3000; y = 4000; } var c = new C(); } C =…
Daniel Nastase
  • 1,035
  • 2
  • 8
  • 6
101
votes
17 answers

Overriding vs Hiding Java - Confused

I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused. To be more clear, I understand overriding well. My issue is that I…
Lostlinkpr
  • 1,453
  • 2
  • 12
  • 13
99
votes
11 answers

Custom ImageView with drop shadow

Okay, I've been reading and searching around, and am now banging my head against the wall trying to figure this out. Here's what I have so far: package com.pockdroid.sandbox; import android.content.Context; import android.graphics.Canvas; import…
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
98
votes
15 answers

C#: Overriding return types

Is there way to override return types in C#? If so how, and if not why and what is a recommended way of doing it? My case is that I have an interface with an abstract base class and descendants of that. I would like to do this (ok not really, but as…
Svish
  • 152,914
  • 173
  • 462
  • 620
96
votes
4 answers

How to override to_json in Rails?

Update: This issue was not properly explored. The real issue lies within render :json. The first code paste in the original question will yield the expected result. However, there is still a caveat. See this example: render :json => current_user is…
maček
  • 76,434
  • 37
  • 167
  • 198
92
votes
4 answers

Overriding "+=" in Python? (__iadd__() method)

Is it possible to override += in Python?
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
91
votes
3 answers

C++ virtual function return type

Is it possible for an inherited class to implement a virtual function with a different return type (not using a template as return)?
Bob
  • 10,741
  • 27
  • 89
  • 143
91
votes
8 answers

How to force derived class to call super method? (Like Android does)

I was wondering, when creating new Activity classes and then overriding the onCreate() method, in eclipse I always get auto added: super.onCreate(). How does this happen? Is there a java keyword in the abstract or parent class that forces this? I…
Peterdk
  • 15,625
  • 20
  • 101
  • 140
91
votes
7 answers

@Override is not allowed when implementing interface method

I have the problem mentioned in the title. You could say that this thread duplicates another one: How do I turn off error validation for annotations in IntelliJ IDEA? But the solution given there doesn't work. They say that I need to take the…
Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67
90
votes
2 answers

TypeScript override ToString()

Let's say I have a class Person which looks like this: class Person { constructor( public firstName: string, public lastName: string, public age: number ) {} } I have overridden the toString method as follows. public…
Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
86
votes
5 answers

Can I override a property in c#? How?

I have this Base class: abstract class Base { public int x { get { throw new NotImplementedException(); } } } And the following descendant: class Derived : Base { public int x { get { //Actual Implementaion } } } When I compile…
atoMerz
  • 7,534
  • 16
  • 61
  • 101