Java Interview Questions

Are you confused about how to get ready for your following java interview? Then these java interview questions will surely go to help you out.

In the recent few months, I take part in a few java interviews, and I faced difficulty with some questions, so I have written this post, so if you are going to appear in a java interview, you can make sure you are prepared. Also, I hope this post will help you with a last-minute java revision.

So let’s start.

1. What are the fundamental concepts of OOPs in Java?

  • Class: It is a collection of objects which can have common properties. It is a template or a blueprint from which objects are created and accessed. No memory space is allocated to a class. It is a logical entity. It can’t be physical.
  • Object: An object is an instance of a class with a state, identity, and behavior. Memory space is allocated to the object when created. It can be physical or logical.
  • Encapsulation: It is the process of wrapping code and data under a unit. In Java, we can create a fully encapsulated class by making all the data members of the class private. It leads to data abstraction and data hiding.
  • Abstraction: It is the process of hiding unnecessary details or implementation details from the outside world. It reduces complexity.
  • Inheritance: It is a mechanism through which one class gains all the properties and behaviors of another class. The primary purpose of using inheritance is code reusability. Code reusability means building new classes based upon existing classes. It represents the IS-A relationship.
  • Polymorphism: Poly means having many forms, so polymorphism implies a message’s ability to be displayed in many forms. In polymorphism, every object will have its implementation of the method.
  • Message Passing: It is a mechanism that allows processes to communicate with each other without sharing the same address space. In Java, we can see it like sending an object, i.e., a message from one thread to another. It is used for thread communication and in parallel computing. Message parsing can be synchronous or asynchronous.
  • Method: It is a collection of statements that perform a particular task or operation and only run when called.
crop businessman giving contract to woman to sign
Interview | Source: Pexels

2. How to access the members of the parent class?

Using the super keyword, we can access the data members and member functions of parent class in a child class as it refers to the objects of an immediate parent class. It eliminates the confusion between the superclasses and subclasses method with the same name. Also, super keyword used to invoke member function and constructor.

3. Difference between abstract and interfaces.

Interfaces Abstract Classes
We use an interface to build loosely-coupled, extensible, testable applications.We create a class as abstract when we declare class but don’t want to create an object.
It is a contract.It is a partially completed class.
It can have abstract methods, static and default.Abstract and non-abstract methods are there.
Interfaces emphasize what we should do.Abstract classes emphasize how we should do.
It can be implemented using the implement keyword.Abstract class inherited using the extend keyword.

4. State some interface features we should not use.

  • Declaring fields in an interface as they are public final static fields. So they are unnecessary implementations that can affect our dependent classes.
  • Avoid using static methods in interfaces because we are dealing with how? And how meant for classes, not for interfaces.
  • Not use private methods in interfaces because the only implementation should be there in interfaces.
  • Using interfaces to achieve multiple inheritances is a bad practice as it brings lots of complexities.

5. Difference between method overloading and overriding.

Method OverloadingMethod Overriding
Overloading means a class has different implementations of the same method using other parameters.Overriding means the child class has the same method as declared in the parent class.
It is used to achieve compile-time polymorphism.It is used to achieve runtime polymorphism.
It occurs within the class.It occurs in two classes.
In overloading, a parameter must be different.In overriding, a parameter must be the same.
It is used to increase the readability of the program.It is used to provide the specific implementation of the parent class method.

6. Why is string immutable in Java?

String in Java is immutable because the String class in Java is final. And we cannot extend final classes. So, once we create or initialize a string, we cannot change it. The methods like toUpperCase(), toLowerCase() returns a new string rather than changing the older one.

7. What is a singleton class?    

Singleton is a class that has only one object at a time. It is used when only a single instance of a class is required throughout the program and if we try to instantiate another object after the first instance is created, then the new variable also points to the first one. So this class should not have multiple instances.

black magnifying glass
FAQ | Source: Pexels.com

8. Why multiple inheritances are not supported in Java?

To reduce the complexities and to simplify the languages, multiple inheritances are not supported. If two superclasses A and B, having a similar method, printName() extends C. Now at the time of invocation, the Java compiler cannot decide which printName() method it should invoke so, there will be ambiguity.

9. Why is Java not a fully object-oriented language?

Java language is not fully object-oriented because for a language to be fully object-oriented, it should have seven qualities, one of which is, all user-defined and primitive types should be objects. But Java has pre-defined data types that are not objects like int, char, boolean.

Second, when we declare a class as static, we can access its member without creating an object or using(.) dot defying another object-oriented quality.

10. What is the difference between final, finally, finalize?

FinalFinallyFinalize
The Final is a reserved keyword.Finally is a block used in the try-catch block.Finalize is a method.
It is used to restrict classes, variables, and methods and cannot extend the final class, cannot override a final method, and cannot change the final variable’s value.It is used to place an essential program because this block will always get executed in the last, even if we have exceptions or not.The garbage collector calls this method just before garbage collection to perform the clean-up process like de-allocation of the particular object’s resources.

Hopefully, you found all these Java Interview questions helpful. So, now it’s your turn comment down below the java questions you faced in your interview.

Some links on this page are affiliate links. This means that if you choose to make a purchase, we may earn a small commission at no extra cost to you. For more information, Go here

Leave a Reply

Your email address will not be published.