Tuesday, July 21, 2009

Interface legal declaration rules and issues

Interfaces follow certain rules which you need to know in order to become an Java expert.There are two rules mainly for interfaces implementation and declaration.

1.A class can implement more than one interface.That is the following declaration is perfectly legal:-

public class Subaru implements Colorable,speedable{// Horrible code here}

It is evident that you can extend only one class ,but you can implement many interfaces.The implemented interface describes the role the class will play.

2.An Interface can extend other Interfaces,but never implement anything.
Following is perfectly legal:-

interface Colorable extends Paintable{ ... }

This requires some thinking from you for a moment.Ok,lets see what happens in this case,here the first concrete (non abstract) implementation class of Colorable must implement all the methods from Colorable and also we have to implement all the buck of Paintable interface.

IMPORTANT:-An Interface can extend more than one Interface.

whoa!! this is strange we have never seen a class who can extend more than one class but what a mess Interface does this.It needs severe attention ...

For example :-

interface Colorable extends Paintable,Sprayble,Movable{ ... } // Perfectly legal

public class Subaru implements Colorable { ... }

The more interfaces the implemented interface will extend more messy it will for first concrete implementation of the interface.That is it has to implement all the methods of Colorable and also of all other extended interfaces.In this case of Paintable,Movable,Sprayble.

It doesn't matter whether you provide some thing inside implemented methods.You can left the implemented methods empty.

If the first implementation is an abstract class then the first concrete subclass of abstract class has to do all the work,implementation of all the methods.

Other Interface tutorials:-

Interface declaration rules
Interface rules and explanations
Why Java not support multiple inheritance

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP