Tutorials

Java OOP Interview Question


Java OOP Interview Question set4


Can we instantiate abstract class?

An abstract class cannot be instantiated.

We cannot create object of an abstract class.

 

This is an important point to note.

If you want to have a class that you don't want to be instantiated at all by other classes then declare it abstract.


What is an interface?

An interface in java is used to provide complete abstraction.

You can create an interface in java using 'interface' keyword as shown in example below.

 

An interface has all the methods by default abstract. It means that interface does not contain any method whose implementation is already provided. The class implementing the interface needs to provide implementation for each abstract methods declared by the interface. If a class doesnot follow this rule then it needs to be declared as abstract class.

 

An example of interface is shown below:

interface SampleIntrfc{

   public static final int constVariable = 12; 

   public void methodA();
   public void methodB();

}

To learn in depth about interface, facts on interface, understanding the example and other detail click here


Can a class extend interface?

No, a class doesnot extend an interface.

Rather, a class implements an interface as shown below:

class ABC implements SampleEx { 

    public void method1(){

         System.out.print("Implementation provided");

   }
      
    public void method2(){
         System.out.println("Some more tasks by A class");
    }
}

Here SampleEx is an interface.

class ABC implements interface SampleEx.

 

When a class implements an interface then it must provide implementation to all of its abstract methods.

If not, then the class should be declared abstract.

To learn in details click here


How will you decide whether to use abstract class or interface in java?

To be updated soon...

 



Please Share this page
Views : 43
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!