Monday, 11 January 2016

Interface and Abstract Class Interview Questions and Answers

1)  When to use abstract class and when to use interface in Java?
    
      Please refer this link 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 an interface be final in Java?

      Not possible, doing so will result compilation error.

  
8)  Can abstract class have static methods in Java?

      Yes, abstract class can declare and define static methods.

 
9) 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.


10)  Can abstract class contains main method?

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


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

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


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

      No, interfaces don't extend Object class.


13) Can we declare abstract methods as synchronized?

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


14) 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.


15) 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".  

5 comments:

  1. Hey Anil Nivargi,

    Thank you! Thank you! Thank you! Your blog was a total game changer!

    I am going to
    implement the blackbox class "messagePool". Details are as below. I
    just want to implement it so that the whole application can run.

    // Call messagePool

    Java Code:

    Dac dac =
    GenericDac.getInstance();


    String result =
    dac.getMessagePool().getMessage(languageCode, key);

    public abstract interface Dac extends java.io.Serializable{


    public abstract void init(javax.servlet.ServletContext
    arg0, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3,
    java.lang.String arg4) throws com.jos.adf3.core.exception.DacInitException;


    public abstract com.pool.MessagePool getMessagePool();


    }


    public class GenericDac implements Dac{


    private com.pool.MessagePool messagePool;


    }



    Super likes !!! for this
    amazing post. I thinks
    everyone should bookmark this.

    Regards,
    Morgan

    ReplyDelete
  2. Thank you for sharing this wonderful blog. It will help to improve my knowledge about JAVA frameworks. This blog will know more about JAVA concepts. Also, it will improve my programming skill.
    Spring Training in Chennai | Spring framework Training | Spring framework Certification | Spring Hibernate Training in Chennai

    ReplyDelete
  3. I am really happy with your blog because your article is very unique and powerful for new reader.
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  4. Nice blog..It was informative and very helpful..Thanks for sharing.keep update selenium training in velachery
    best selenium training in chennai

    ReplyDelete