Pre-requisites -
- Post#118 Interfaces
- Post#119 Class implementing Multiple Interfaces
- Post#120 Single Interfaces implemented by Multiple Classes
- Post#121 Super Class implementing an Interface and Sub Class Inheriting it
- Post#122 Sub Class implementing an Interface
- Post#123 Implementing the Interface containing variables
- Post#124 Accessing the Overriding method of sub-class using super-class object
- Post#125 Interface References
- Post#128 Abstract Classes can partially implement the Interfaces
- Post#129 Interface Inheriting another Interface
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.
No comments:
Post a Comment