Monday, February 24, 2014

Interface Interview Questions

1.What is Interface? 

You can only declare the method here, You can not implement here.

2.Why do we want to use Interface?

Java does not support multiple inheritance. Instead of that, we can use interface. Next thing is, it will               allow the third party to implement its methods.

3.Simple interface example

interface JavaHit {
void test(); //you can only do method declare here.
}

Important Note: After compilation, that method declaration will changed like
public abstract void test();
Because of, that interface should allow thaird party to implement that. So its giving access like public.

4.Can you write one interface example?

interface JavaHit {
void connect(); //it will change to public access after compilation.
oid disconnect();
}

class SqlDb implements JavaHit {
public void connect() {
System.out.println("SQL Database Connected");
}
public void disconnect() {
System.out.println("SQL Database Disconnected");
}
}
class OracleDb implements JavaHit {
public void connect() {
System.out.println("Oracle Database Connected");
}
public void disconnect() {
System.out.println("Oracle Database Disconnected");
}
}

5.Can I create methods inside interface with private or protected access?

No. public only allowed. Refer: Question 3

6.How do I create an object to interface?

You can not create object to interface class.

7.An interface can extend another interface?

Yes. Possible.

8.An interface can implement another interface?

No.

9.Can I write a class inside an interface?

Yes.

No comments:

Post a Comment