121. Superclass implementing an Interface and Subclass Inheriting it

No need of implementing an Interface in Subclass if the Superclass has already implemented the interface. If Superclass has already implemented an interface, the subclass can inherit it without implementing the interface again.

Lets implement this on Eclipse IDE:

1. Create Interface 'Interface1' as shown below:

2. Create 'Superclass' class which implements the 'Interface1' as shown below:

3. Create 'Subclass' class (i.e. subclass of 'Superclass' class) which don't implement the 'Interface1' as the 'Interface1' has already implemented it as shown below: (subclass will inherit the implemented Interface1 )

4. Create 'HierarchyInterfaceDemo' class to access the method of  Interface1 which is implemented in the Super and Sub classes as shown below:

5. Save and Run the 'HierarchyInterfaceDemo' 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' and 'Subclass' 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.

'Subclass implementing an Interface' will be explained in the next post.

1 comment:

Moni Mathur said...

Arun in the "HierarchyInterfaceDemo" class why did we create two objects for superclass as well as subclass.the motive could have been achieved by just creating an object for subclass as the subclass inherits the superclass properties.

Followers :)

Blog Index