Java Interview Questions

Java Interview Questions

Below are most frequently asked question in Interview for Java topic. Now a days expectation had been increased for an testers and many projects are moving to Devops with Agile Methodology where each team member had equal visibility to the client , So it is expected from testers that being not just a manual tester they should know about the Automation tools, at least any one programming language and Devops tools so that they can also be a equal contributor in team.

1.What is object in Java?

Object is an instance of class which has a state and behavior. We can create any number of objects of a class.

2.What is a Class in Java?

A class can be defined as a template/blueprint that describes the behavior/state of the object of its type.

3.What are the different types OOPS concepts you know?

Different OOPS concepts are :

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
4.What is inheritance in Java?

Inheritance can be defined as the process where one class acquires the properties of another class.The class which inherits the properties of other is known as subclass/child class and the class whose properties had been inherited is known as super/parent class.

5.Can we access the members of child class when object is created using super class reference?

No, We cannot access member of child class when object is created using super class reference.
Fruit //parent class
Apple // Child class
Fruits demo = new Apple(); //object creation

Here using “demo” object you can access variable and methods of parent class only (Fruit class) but not child class (apple) variable and methods, however if you have used same method name and variable name in child class which are already present in parent class then method of parent class will be overridden (Dynamic polymorphism) and variable will not be overloaded as it is not allowed in Java.

6.Explain super keyword in Java?

Super keyword is used in Java for below two reasons :

To differentiate the members between super class and sub class ,if the members of the super class have the same names as that of sub class, to differentiate these variables and methods we use super keyword.
super.a; //using variable of super class
super.add(); //calling super class method
Invoking the super class constructor from subclass.
Fruit //Super class
Apple //Child classIn the child class constructor we will write below code , when constructor of child class is called super class constructor will also be get called implicitly if we have not called it explicitly and code written in site it will be executed first.
public class Apple extends Fruit {
Apple(int age) { //child class constructor.
super(age); //calling super class constructor.

7.Can we override private methods of super class?

No, we cannot override private or final methods of super class.

8.Can we override super class method when subclass is in different package?

Yes, we can override method when subclass is in different package with condition that , super class methods are public/Protected and not final or private.

9.Can we override constructor?

No, we cannot override constructor.

10.Is method overloading possible by changing the return type of method only?

No, method overloading is not possible by changing only the return type of method.

11.Can we overload java main() method?

Yes, we can overload main method but JVM calls main() method which receives string array as arguments only.

12.Can you override a private method in Java?

No,its not possible to override private method in Java because it is not accessible in child class.

13.How can you stop any one to create an object of any class outside it?

By declaring the constructor of the class as private.

14.How will you achieve HAS-A relationship?

We can achieve HAS-A relationship by help of aggregation. When any object contain one or more object inside it.
class Apple(
int a;

class Box {
int id;
String name;
Apple obj2; //Apple is a class.

15.Why we use aggregation?

To reuse the code and when there is no IS-A relation then we use aggregation.

16.Inheritance denotes which type of relationship?

Inheritance denoted IS-A relationship.

17.Can we overload the final method in Java?

Yes, we overload the final method in Java.

18.Can we override an abstract method?

Yes ,is mandatory to override abstract method in implementing class and we can override if required in extending class.

19.Can you override a method in the same class?

No, we cannot override the method in the same class.

20.Can a main() method be declared final?

Yes, we can declare main method final. If we make this final it can not be overridden and compiler will still find it.

Leave a Reply

Your email address will not be published. Required fields are marked *