The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 10 interview questions on java interfaces

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 10 interview questions on java interfaces Posted: Mar 1, 2016 7:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 10 interview questions on java interfaces
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.What is an interface in Java.

  • 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. 
  • We can develop interfaces by using "interface" keyword.  
  • A class will implements all the methods in an interface.



  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x=12;
  5. void show(); 

  6. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }

  7. }



  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. int x=12;
  5. void show(); 

  6.   
  7. default void display(){
  8.  
  9. System.out.println("default method of interface");
  10.  
  11. }
  12.  
  13. Static void print(String str){
  14.  
  15. System.out.println("Static method of interface:"+str);
  16.  
  17. }
  18.  
  19.  
  20. }


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

Interface concrete method in java


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.

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error


  5. }


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.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error: The blank final field y may not have been initialized


  5. }
5.Can we declare interface members as private or protected?
  • No.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
  5. public, static & final are permitted
  6. protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
  7. public, static & final are permitted

  8. }


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

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }
  7. public static void main(String args[]){
  8.  
  9.  JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
  10.  
  11. }
  12. }

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
declare interface final in java



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.

final interface interview freshers


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() 
  • No need of overriding default and static methods.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface { // The type Example must implement the inherited
  3. abstract method JavaInterface.show()
  4.  
  5. public static void main(String args[]){
  6.  

  7. }
  8. }

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.

Read: Top 10 interview questions on java interfaces

Topic: Java Tutorial : Java String(String Constant pool)- Playlist1 Previous Topic   Next Topic Topic: Tutorial: Correct SLF4J logging usage and how to check it

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use