Learn Java Tutorials Questionwise,Java Tutorials- Questions And Answers -3-Interface

No comments
Here is another article explaining the core java topics in the questions answers form.In this article we have provided some basic questions ans answers about interfaces in java, we will have a broad look into this topic in our upcoming series of question answers. We have covered almost all the basic concepts about interfaces in java with the help of different questions,what is an interface,how to declare an interface in java? and much more.But still if you have some doubts regarding the topic explained in the article then feel free to comment your queries in the comment box.

Q.1 What is an interface in java?

Answer- In java an interface is an 100% abstract class having only abstract methods and constants.When we define an interface we define a contract for what a class can do ,but how class will do it ,it totally depends upon the class. An interface can be implemented by any class from any inheritance tree and only an interface can extend another interface.
Whether we define it or not while declaring an interface in java ,but an interface is always abstract.

Q.2 What is the difference between an interface and abstract class in java?

Answer-

Interface Abstract Class
Only abstract methods are allowed in an Interface Both abstract and non abstract methods are allowed in an abstract class
Interface can extend another interface. A class can never extends an interface
An interface is implemented by a class An abstract class is extended by another class.
An interface can have only static and final variables An abstract class has no such restriction
No need to use abstract keyword while declaring an interface or any of its methods We always need to use abstract keyword while declaring an abstract class and for methods if methods are abstract.

Q.3 Can we declare variables inside Interface if yes then what kind of variables are allowed within an interface?

Answer-we can declare only static final variables inside an interface ie constants.All the classes implementing the interface will have the same value of the variables declared inside an interface.

Q.4 How to declare an interface?

Answer-

We can define an interface using interface keyword with its name like-
interface Bouncable{
//abstract methods
}
OR

public abstract interface Bouncable{
//abstract methods
}



We can or can’t use abstract keyword while declaring an interface because an interface is always public abstract by default. So both the above declarations of interface are correct.

Q.5 Is it necessary to use abstract keyword while declaring an interface? Is it illegal to use abstract keyword while declaring an interface? Why?

Answer-No, it is not necessary to use abstract keyword while declaring an interface in java because as described in the previous answer an interface is always public and abstract by default. But it is always legal to use abstract while declaring an interface.

Q.6 What if we don’t define any modifiers before declaring a method or variable inside an interface?

Answer- If we don’t use any modifiers while declaring an interface method or variable then by default-
An interface method is always public abstract.
An interface variable is always final static.


No comments :

Post a Comment