hi all i tried one quiz in that i got question

Given: 1. public class Blip { 
2. protected int blipvert(int x) { return 0; } 
3. } 
4. class Vert extends Blip { 
5. // insert code here 
6. } Which methods, inserted independently at line 5, will compile?


A. private int blipvert(int x) { return 0; }
 B. private int blipvert(long x) { return 0; } 
C. protected long blipvert(int x) { return 0; } 
D. protected int blipvert(long x) { return 0; }

the answers are B & D , I taught it was is C,D could any one explain me how its working
thanks in advance

So lets see each option:

A: private int blipvert(int x) { return 0; }

Since the signature matches the one in base class method overriding comes into action. BUt the problem is you you cant lower the accessibility of a method i.e you can assign override a protected method as a Private one. But you can make it public or protected itself. Thats why this wont compile. The compiler will say that you are assigning a weaker access specifier.

B:private int blipvert(long x) { return 0; }

This will perfectly compile because here method "overloading" comes into action because the base class and this method differ in their argument type. So based on type of argument type the method will be called.

C. protected long blipvert(int x) { return 0; }

This wont compile because you are creating virtually the "same" methods but you are varying the return types. But there's a catch here. You can vary return types if the types can be safely casted. eg. double and int etc... Here that is not possible. Hence you get a compile time error.

D. protected int blipvert(long x) { return 0; }

This will perfectly compile and method overloading comes into action.

thanks stevanity ,
I missed to check the argument list,
thanks for your explanation ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.