Google+

128. Abstract classes can partially implement the Interfaces






If a class includes an interface but does not fully implement the methods defined by the interface, then the class must be declared as abstract.

Lets implement this on Eclipse IDE (Interface defines four methods but the abstract class implementing the interface only implements one out of four methods without any errors)

1.Create Interface 'Interface1' which defines four methods as shown below:



2. Create abstract class 'Superclass' which implements the Interface1 but only implements a single method 'add( )' of Interface1 as shown below:



3. As we cant create an object for abstract class 'Superclass', lets create subclass 'Subclass' for Superclass as shown below:




4. The following error message asking to implement all the unimplemented methods in Super Class of this Sub Class is displayed -



5. Implement all the methods in the SubClass that are not implemented in the SuperClass as shown below and observe that the error got resolved -


6. Create class 'PartialImplementationDemo' to create an object for 'Subclass' to access the methods inherited and implemented by the Subclass from abstract Superclass as shown below:



7. Save and Run the 'PartialImplementationDemo' class
8. Observe that the output is displayed in the console as shown below:






Please comment below to feedback or ask questions.

'Interface inheriting another Interface' will be explained in the next post.



No comments: