Google+

216. Interface implemented methods must be declared as public







Pre-requisites -


When a Class implements an interface and implements the unimplemented method of the Interface in the Class, we must define the implemented methods of Interface in the Class as public. 

Lets implement this on Eclipse IDE -

1. Create a new Java Project 'Project 43' as shown below -


2. Create an interface say 'Interface1' as shown below -


3. Define a method inside an interface as shown below -


4. Create a Class say 'Class1' as shown below -


5. Implement the interface 'Interface1' in the Class 'Class1' as shown below -


6. View the error displayed in the Class after implementing the Interface and select 'Add Unimplemented Methods' option in the error message as shown below to resolve the error message -


7. Observe that the method defined without Body in the Interface1 is displayed here with the body as shown below -


8. Observe that the method is defined as public as shown below -


9. Now lets remove the public access specifier as shown below and observe that an error is displayed as shown below -


10. View the error message and select 'Change method visibility to public' option in the error message as shown below -


11. Observe that the method is defined again as public and the error message got resolved as shown below -


Hence all the methods of Interface that are implemented in any Class need to be declared as public only.






Please comment below to feedback or ask questions.

Implementing the Nested Interfaces will be explained in the next post.








No comments: