user979431
user979431

Reputation: 357

What does it mean to @override the method of a super class?

Let's say I have a method called mymethod()

and this method overrides the method of the super class method.

What does it mean to override a method?

Does that mean mymethod() ignores everything that is in the method of the superclass, or does that means mymethod() also includes everything in the superclass method?

When overriding a method, can I only override the methods of the same name, or I can override methods of any name?

thanks.

Upvotes: 3

Views: 15101

Answers (6)

wkl
wkl

Reputation: 79901

An example:

public class Base {
    public void saySomething() {
        System.out.println("Hi, I'm a base class");
    }
}

public class Child extends Base {
    @Override
    public void saySomething() {
        System.out.println("Hi, I'm a child class");
    }
}

Now assume we have a main function somewhere...

public static void main(String [] args) {
    Base obj = new Child();
    obj.saySomething();
}

When this runs, it will call Child's version of saySomething, because you overrode the parent's version by giving a new version of the function in Child.

The @Override annotation allows other developers (and you, when you forget) to know that this method overrides something in a base class/interface, and it also allows the compiler to yell at you if you're not actually overriding anything in a base class. For example, if you got the number of arguments wrong for a function, the compiler will give you an error saying your @Override is incorrect.

For example:

public class Child extends Base {
    @Override
    public void saySomething(int x) {
        System.out.println("I'm a child and x is: " + x);
    }
}

The compiler will yell at you because this version of saySomething takes an argument, but the parent's version doesn't have an argument, so you're @Override-ing something that's not in the parent.


On super

The Child version of saySomething will not invoke the Base version, you have to do it yourself with super.method().

For example:

public class Child extends Base {
    @Override
    public void saySomething() {
        super.saySomething();
        System.out.println("I'm also a child");
    }
}

If you ran the main and used this Child class, it would print out I'm a base and I'm also a child.

Upvotes: 6

Matt
Matt

Reputation: 4775

When you override a method of the super class, calling that method on your object calls its method instead of that of the super class.

You can call the super class's method (despite having overridden it) using super.methodName(). A common reason for this is when the overridden method would otherwise reimplement the super class method and add additional code specific to the extending class (public void methodName() { super.methodName(); /* more code */ }).

@Override annotation allows you to cause warning at compile time if the method isn't actually overriding anything. It isn't necessary, but these warning are a hit to you that you might have got the signature wrong in the extending class, forgot to implement the method at all in the super class, or some other silly mistake.

Upvotes: 0

TofuBeer
TofuBeer

Reputation: 61526

I once had a student come to ask me why his code wasn't working. He had spent several days wondering why he could put something into a collection but was not able to find it. His code was something like:

public int hashcode()

instead of:

public int hashCode()

So the hashCode method never got called.

Adding @Overrides to a method makes it clear that you are overriding the method AND make sure that you really are overriding a method.

Upvotes: 0

Gyan
Gyan

Reputation: 12410

What does it mean to override a method?

It means you replace the super class definition of the method with your own definition.

does that mean mymethod() ignores everything that is in the method of the super class? or does that means mymethod() also includes everything in the superclass method?

You can choose whether to include the super class definition within your definition. To include it, you need to call super.mymethod() within mymethod().

and when overriding a method, can I only override the methods of the same name, or I can override methods of any name?

To override a method, you must supply a method in the sub class with the same signature (which means the same name, parameters and return type).

As a side note, the @Override annotation in your question does not actually cause your method to override another method. It causes a compile-time error if a method annotated with it does not have a signature matching a public or protected method of a super class (or interface as of 1.6).

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95518

Overriding means that when you call a method on your object, your object's method is called instead of the super class. The @Override annotation is something you use to make sure that you are overriding the correct method of the superclass. If you annotate a method that does not exist in the superclass, the Java compiler will give you an error. This way you can be sure that you are overriding the correct methods. This is especially useful in cases like this:

public class MyClass {

    ...

    public boolean equals(MyClass myClass) {
       ...
    }
}

There is a logic-bug in the code above. You haven't actually overridden the Object class's equals method. If you add the @Override annotation:

public class MyClass {

    ...

    @Override
    public boolean equals(MyClass myClass) {
       ...
    }
}

The Java compiler will now complain because there is no corresponding method in the parent class. You'll then know that the correct solution is:

public class MyClass {

    ...

    @Override
    public boolean equals(Object o) {
       ...
    }
}

To call the parent class's method, you can call super.overriddenMethod() where overriddenMethod is the name of the method you have overridden. So if you want to do something in addition to what the parent class already does, you can do something like this:

public class MyClass {

    ...

    @Override
    public void overriddenMethod() {
        super.overriddenMethod();

        /* whatever additional stuff you want to do */
    }
}

Upvotes: 4

Joshua Smith
Joshua Smith

Reputation: 421

If an inheriting class has on override method of the same name as the parent class it will be called instead of the one in the parent class. This only works if the names are the same, and of course if the signature of the method matches your call to the method.

Upvotes: 1

Related Questions