Monday, 11 January 2016

Interface and Abstract Related Interview Questions and Answers

1)  When to use abstract class and when to use interface in Java?
    
      Please refer this link http://www.connect2java.com/interviewquestions/when-we-use-interface-and-abstract-class-in-java/


2) What modifiers may be used with an interface declaration?

      public & abstract.


3) Can Abstract class  have a Constructors? Can interfaces have a Constructors?
      
      Abstract class's can have a constructor but you can not access it through the object, since you can not instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.

             public abstract class AbsExample {
                       public AbsExample() {
                               System.out.println("Abstract class constructor example");
                       }
             }
             public class Test extends AbsExample {
                     public static void main (String[] args) {
                              Test test = new Test();
                     }
            }
Interface can not have the constructor.


4) If interface and abstract class have same methods and those methods contain no implementation, which one would you prefer?
      
        Obviously you should go for an interface. Implementing an interface is for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass.


5) Can abstract class implements interface in Java? does they require to implement all methods?
       
        Yes, abstract class can implement interface by using implements keyword. Since they are abstract ,they don't need to implement all methods, in sub class that can extends abstract class to need to implement methods.
Example:
                  public interface example1{
                            public String getName();
                  }
                  public abstract class AbstractEx implements example1 {
                  }

                  public class ExampleOfInterface extends AbstractEx{
                           public String getName() {
                                    return "Anil";
                          }
                  }

         In the above code, won't give any error. Because in abstract class it's not necessary to implement interface abstract methods. In sub class you have to implement all methods.


6)  Can abstract class be final in Java?

      No, abstract class can not be final in Java.  Making them final will stop abstract class from being extended.


7)  Can abstract class have static methods in Java?

      Yes, abstract class can declare and define static methods.

 
8) Can you create instance of abstract class in Java?

     No, you can not make instance of the abstract class in Java. because it's incomplete.


9)  Can abstract class contains main method?

     Yes, abstract class can contain main method, its just another static method.


10) Is it necessary for abstract class to have abstract method?

      No, it's not necessary for abstract class to have abstract method.


11) Like classes, does interfaces also extend Object class by default ?

      No, interfaces don't extend Object class.


12) Can we declare abstract methods as synchronized?

     No, we can not declare abstract methods as synchronized. But overridden methods you can declare as synchronized.


13) What is the output of below code?
      
                 class Example{
                        abstract void start();
                 }

Answer:- Compile time error. If there is any abstract method in a class, that class must be abstract.


14) If we declare a concrete method in an interface , what will happen ?

          public interface InterfaceExample {
                   public void display () {              //compile time error
                   }
          }

In an interface all methods should be abstract, if we declare concrete method , it will give compile time error that "Abstract methods do not specify a body".