user3170491
user3170491

Reputation: 33

how overriding works in the following code?

consider the following code in java:

public class Superclass {    
    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {    
    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

The output obtained is the following:

Printed in Superclass
Printed in Subclass

If the subclass is overriding why is superclass println still displaying on screen? It is not suppose only to display the content of the method on the subclass?

Why is super not being invoked in the following code if the method is overriding?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Upvotes: 1

Views: 131

Answers (5)

if the subclass is overriding why is superclass println still displaying on screen? it is not suppose only to display the content of the method on the subclass?

This line:

super.printMethod();

Will call the super class's printMethod(). This will essentially become:

new Superclass().printmethod();

You don't need to call super to do an override. Do this instead:

// overrides printMethod in Superclass
public void printMethod() {
    //super.printMethod();
    System.out.println("Printed in Subclass");
}

Another way to approach your question is to do the following:

public class Superclass {    
    public void printMethod() {
        System.out.println("Printed in " + this.getClass().getName() + ".");
    }
}

Then just do:

public class Subclass extends Superclass {    
    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();//just do override
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

another question, why is super not being invoked in the following code if the method is overriding?

You don't need super to do @Override as mentioned above. From this question:

Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.

Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements), but it's better than nothing.

If you want to know more about when to use super with method overrides, then check this article question.

Upvotes: 1

William Morrison
William Morrison

Reputation: 11006

  • if the subclass is overriding why is superclass println still displaying on screen?

    Because you called super.printMethod(). That calls the parent class implementation of printMethod().

  • why is super not being invoked in the following code if the method is overriding?

    Because the developer didn't need, or didn't want the code in the parent class to execute- he wanted to rewrite the method entirely. You might also see the override keyword used when the parent implementation isn't defined, such as with an interface or an abstract class.

    Calling the parent class implementation of a method isn't required.

Upvotes: 2

La-comadreja
La-comadreja

Reputation: 5755

When you invoke super(), you call the superclass method. The override would still work without "Printed in Superclass" printed if you simply deleted the super() invocation.

Upvotes: 0

rgettman
rgettman

Reputation: 178343

You don't have to call the superclass method with super.printMethod(); to override the method, but you did, so

Printed in Superclass

is printed. To not have it print, remove the call to super.printMethod();; it's not necessary.

For onCreateOptionsMenu, it's not using the overridden functionality, so there's no need to call super.onCreateOptionsMenu.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

1 Because of the first line in the method super.printMethod();

2 Because there is no call to a super method.

Upvotes: 2

Related Questions