[HD Pg 0, Sec. 13.3.3 - quiz]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
agatha
Posts: 4
Joined: Tue Nov 19, 2019 7:01 pm
Contact:

[HD Pg 0, Sec. 13.3.3 - quiz]

Post by agatha »

for 13.3.3 after erasure should c be correctly override the base class? After all the <…> is stripped

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 13.3.3 - quiz]

Post by admin »

That's correct. After erasure (i.e. at runtime) both the given methods have the same signature and that is exactly what confuses the compiler. For a successful override, the signature of the method (at compile time) must be an exact match.
The compiler understands that the signatures of the given methods are same after erasure (which means it is an override at runtime) but not same at compile time (which means it is not an override but an overload at compile time). This contradiction causes the compiler to reject the code.

There is one exception to this rule (which is not important for the Part 1 exam). The exception is that if the signature of the base class method after erasure matches with the subclass method without erasure, then it is a successful override. For example, the following is a valid override:

Code: Select all

class Base{
public List<String> processList(List<String> list){ 
     return null; 
  }
}

class SubClass extends Base{
  public List processList(List list){   //valid override because after erasure, it matches with the base class method.
    return null; 
  }
}
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests