1.What is an interface in Java.
2.What will happen if we define a concrete method in an interface?
3.Can we create non static variables in an interface?
4.What will happen if we not initialize variables in an interface.
5.Can we declare interface members as private or protected?
7.When we need to use extends and implements?
6.Can we create object for an interface?
7.Can we declare interface as final?
8.Can we declare constructor inside an interface?
9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?
10.How can we access same variables defined in two interfaces implemented by a class?
- Before Java 8 interfaces are pure abstract classes which allow us to define public static final variables and public abstract methods(by default).
- In java 8 introduced static methods and default methods in interfaces.
- Java 8 Interface Static and Default Methods
- We can develop interfaces by using "interface" keyword.
- A class will implements all the methods in an interface.
- package com.instanceofjava;
- interface JavaInterface{
- int x=12;
- void show();
- }
- package com.instanceofjava;
- Class A implements JavaInterface {
- void show(){
- // code
- }
- }
- package com.instanceofjava;
- interface Java8Interface{
- int x=12;
- void show();
- default void display(){
- System.out.println("default method of interface");
- }
- Static void print(String str){
- System.out.println("Static method of interface:"+str);
- }
- }
2.What will happen if we define a concrete method in an interface?
- By default interface methods are abstract.
- if we declare any concrete method in an interface compile time error will come.
- Error:Abstract methods do not specify a body
3.Can we create non static variables in an interface?
- No.We can not create non static variables in an interface.
- If we try to create non static variables compile time error comes.
- By default members will be treated as public static final variables so it expects some value to be initialized.
- package com.instanceofjava;
- interface JavaInterface{
- int x, y; // compile time error
- }
4.What will happen if we not initialize variables in an interface.
- Compile time error will come because by default members will be treated as public static final variables so it expects some value to be initialized.
- package com.instanceofjava;
- interface JavaInterface{
- int x, y; // compile time error: The blank final field y may not have been initialized
- }
- No.
- package com.instanceofjava;
- interface JavaInterface{
- private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
- public, static & final are permitted
- protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
- public, static & final are permitted
- }
7.When we need to use extends and implements?
- A class will implements an interface.
- A class will extends another class.
- An interface extends another interface.
6.Can we create object for an interface?
- NO. We can not create object for interface.
- We can create a variable fro an interface
- package com.instanceofjava;
- interface JavaInterface{
- void show();
- }
- package com.instanceofjava;
- interface A implements JavaInterface {
- void show(){
- // code
- }
- public static void main(String args[]){
- JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
- }
- }
7.Can we declare interface as final?
- No. Compile time error will come.
- Error: Illegal modifier for the interface Sample; only public & abstract are permitted
8.Can we declare constructor inside an interface?
- No. Interfaces does not allow constructors.
- The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.
9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?
- A class which implements an interface should implement all the methods (abstract) otherwise compiler will throw an error.
- The type Example must implement the inherited abstract method JavaInterface.show()
- If we declare class as abstract no need to implement methods.
- No need of overriding default and static methods.
- package com.instanceofjava;
- interface JavaInterface{
- void show();
- }
- package com.instanceofjava;
- interface A implements JavaInterface { // The type Example must implement the inherited
- abstract method JavaInterface.show()
- public static void main(String args[]){
- }
- }
10.How can we access same variables defined in two interfaces implemented by a class?
- By Using corresponding interface.variable_name we can access variables of corresponding interfaces.
- Yes implementing the method once is enough in class.
- A class cannot implement two interfaces that have methods with same name but different return type.
yes very helpfull for me
ReplyDeleteyes very helpfull for me
ReplyDeletenice for quick revision.
ReplyDeleteVery good:) helpful
ReplyDeletePrecise and logical.. Very helpful..
ReplyDeletethe way of explanation is easy to understand!!
ReplyDeleteit is so much of information can be gather f
ReplyDeleteor me also