Java Interview Questions | Page 2


Question 11:

Define encapsulation in java.

Encapsulation can be described as a wrapper that prevents the code and data being haphazardly accessed by other code residing outside the class.
With the feature, gives maintainability, flexibility and extensibility to our code.

Question 12:

What are the benefits of encapsulation?

  • The field of a class can be made read-only or write-only
  • The class can have total control over what to store and how to store in its fields.
Question 13:

What is an interface?

An interface is a group of related methods with empty bodies. Interface allows a class to become more formal about the behaviour it promises to provide.
Interface form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.
If any class implements an interface, all methods defined by that interface must appear in its source code implemented by the class.

Question 14:

What is abstract class?

An abstract class is a class that is declared abstract – it may or may not include abstract methods. Abstract classes can’t be instantiated but they can be subclassed.
For example:-

 abstract class Demo
 {
         //concrete method
         public void concreteMethod()
         {
         }
         //abstract method
         abstract void abstractMethod();
 }
Question 15:

Can any abstract class be declared as final? If not why?

No, any abstract class cannot be declared as final; because the class is declared abstract so that any concrete class can extend and implement the properties of class whereas any class is declared as final to avoid the class to inherited by any other class.


Question 16:

Define abstract methods.

Methods declared without any implementation and declared as abstract are known as abstract methods.
Note:- Abstract methods can never be final and static.
             Syntax:- abstract return_type function_name();

Question 17:

Can you have a main method in an abstract class?

Yes, we can have main() method in abstract class as abstract classes allows concrete methods.
As there is no-need to create an instance of the class to call main() because it is static.
Hence there is no problem to have main() in abstract class.

Question 18:

Differentiate between abstract class and interface.

1.       Abstract class can have instance method that implements a default behaviour.

1.  Interfaces are implicitly abstract and cannot have implementation.

2.       Any abstract class may contain final or non – final variables.

2. Variables declared in a Java interface are by default final.

3.       Variables declared in Java abstract class may b private, protected, public or default.

3. Variables declared in interface are by default public.

4.       Java abstract classes should be extended using “extends” keyword.

4. Java interface should be implemented using “implements” keyword.

5.       An abstract class can extend another class and implement multiple interfaces.

5. An interface can extend interface only.

Question 19:

Define static polymorphism.

Overloading is static polymorphism. Overloading means two methods having same method name but takes different input parameters(i.e. different number of parameters, different datatypes).
This is static because, which method will be invoked is decided at compile time.

Question 20:

What is method overloading in Java?

Method overloading is a feature that allows a class to have two or more methods having same name, but the argument list is different.
When we say argument list is different, it can be in following ways:-

  • Number of parameters.
  • Data type of parameters.
  • Sequence of data type of parameters.

For example:-

public void arithmetic(int num1, int num2){
}
public void arithmetic(float num1, float num2){
}
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media