0

Below is the code which I am trying to analyze.

class Acc{
    public void aMethod(List<Integer> A){
        System.out.println(A.toString());
    }
}

class Back extends Acc{
    public void aMethod(String A){
        System.out.println(A);
    }
}

Here if I invoke it as

Acc a = new Back();
a.aMethod(list);

But upon debugging the method of parent class is being called.But it should dynamically call method of Back as per my understanding.Please help me understand of this behaviour.Now again If I keep parent class argument as List<Integer> and child one as List<String> then it gives a name clash thing.Why not in this case?

3
  • 4
    You are not overwriting the parent method when the signature of the methods are different! Commented Jun 16, 2015 at 12:13
  • Then why does it gives a compilation issue in the later case mentioned? Commented Jun 16, 2015 at 12:14
  • it has something to do with type erasure for generics, in runtime both methods are seen as aMethod(List list) Commented Jun 16, 2015 at 12:19

2 Answers 2

4

Parent class method public void aMethod(List<Integer> A) and child class method is public void aMethod(String A).

Both have different parameters, so it is not Method Overriding. However in child class it can be thought of as Method Overloading.

When you call the method, you are passing the list parameter and there is only only method that matches this signature and that is of parent class. So the method of parent class gets called.

Method Overriding

For method overriding methods in base class and child class must have same signature i.e. same name and same arguments. However method in child class can have higher scope access specifier than base class's method.

In simpler words, if method in base class is default then method in child class can be public.

Sign up to request clarification or add additional context in comments.

4 Comments

Methods must have the same signature = same name AND same parameters
Then why does it gives a compilation issue in the later case mentioned?
@Nilesh Because Complier doesn't mind Method Overloading even in case of Inheritance. You are just creating another method with some other arguments, so you are basically overloading the method that you got from your base class.
Java generics are implemented with erasure, so method( List<String> a) and method( List<Integer> a ) both compile to -> method( List a )
0

Here are some rules for method overriding:

The argument list should be exactly the same as that of the overridden method.

The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.

The access level cannot be more restrictive than the overridden method's access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.

Instance methods can be overridden only if they are inherited by the subclass.

A method declared final cannot be overridden.

A method declared static cannot be overridden but can be re-declared.

If a method cannot be inherited, then it cannot be overridden.

A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

A subclass in a different package can only override the non-final methods declared public or protected.

An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

If you check this and then code written, it would be clear where its wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.