122. Subclass implementing an Interface

Subclass can directly implement an Interface though the Superclass has not implemented it. See the below example to find out how the Subclass can directly implement an Interface:

class ClassTwo extends ClassOne implements Interface1, Interface2
      //Body of the Subclass

Lets implement this on Eclipse IDE:

1. Create an Interface 'Interface1' as shown below:

2. Create 'Superclass' which don't implement Interface1 as shown below:

3. Create 'Subclass' of 'Superclass' which implements the interface directly instead of inheriting it from Superclass as shown below:

4. Create another class 'SubclassInterfaceDemo' for creating an object to access the members of 'Subclass' as shown below:

5. Save and Run the 'SubclassInterfaceDemo' class
6. Observe that the output is displayed in the console as shown below:

Download this project:

Click here to download the project containing the 'Interface1', 'Superclass', 'Subclass' and 'SubclassInterfaceDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)

Please comment below to feedback or ask questions.

Implementing the Interface containing variables will be explained in the next post.

No comments: