Most commonly asked Constructor interview questions in java
This post cover most commonly asked Constructor interview questions in java.
Why do we need constructor in Java?
Java is an object-oriented language, in which we create and use objects. A constructor is a piece of code similar to a method. It is used to create an object and set the initial state of the object.A constructor is a special function that has same name as class name.Without a constructor, there is no other way to create an object.By default, Java provides a default constructor for every object. If we overload a constructor then we have to implement default constructor.
Why do we need default constructor in Java classes?
Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined. We need at least one constructor to create an object, that’s why Java provides a default constructor.
What is the value returned by Constructor in Java?
When we call a constructor in Java, it returns the object created by it. That is how we create new objects in Java
Can we inherit a Constructor?
No, Java does not support inheritance of constructor.
Why constructors cannot be final, static, or abstract in Java?
If we set a method as final it means we do not want any class to override it. But the constructor (as per Java Language Specification) cannot be overridden. So there is no use of marking it final.
If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.
If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.
Can we mark a constructor as synchronized in Java?
No. We cannot mark a constructor as synchronized.This will lead to compiler error.
The reasoning behind this is that, in this case, only the constructing thread would have access to the object being constructed.
How can you do constructor chaining in Java?
we can’t use this() and super() in a method.
Constructor as a private?
We can declare a constructor as private if we do not want to allow a user to create an object from outside that class. Using this we can restrict the caller from creating objects
If class has an explicit constructor then will it have a default constructor?
No. Compiler places default constructor only if there is no explicit constructor.
In which situation it is mandatory for the developer to provide constructor explicitly?
A developer needs to provide constructor explicitly when one needs to execute some logic at the time of object creation. This logic might be object initializeed logic or some other useful logic.
Access Modifier of Default constructor?
Whatever class have Access Modifier that modifier will be given to default constructor.
Can we define a method with the same name of the class?
Yes, we can define but it will give runtime error.
There is no return type in constructor. but constructor returns a current class instance.
Why is constructor name same as the class name?
The constructor returns a current class object so when we create an object with new keyword and class name. so constructor name would be same as a class name.
The copy constructor is not supported in java
Why a return type is not allowed for constructor?
As there is a possibility of a method having the same name as class name, return type is not allowed in a constructor to differentiate constructor block from method block.
If we place return type in constructor prototype will it leads to Error?
No, because compiler and JVM considers it as a method.
Why Compiler Given Constructor Is Called As Default Constructor?
Because it obtain all its default properties from its class.
Constructor can have throws clause:
we can throw exception from constructor.
It can have logic, as part of logic it can have all java legal statement except return statement with value.
We can not place return in constructor.
It should not contain Non Access Modifiers: final ,static, abstract, synchronized
It can have all four accessibility modifiers: private , public, protected, default
=====================================================================
General question oops static variable
================================================================
Explain the concept of Inheritance?
Which class in Java is superclass of every other class? =====> Object class is super class of every class
Why Java does not support multiple inheritance? ==>
Multiple Inheritance means that a class can inherit behavior from two or more parent classes.
The issue with Multiple Inheritance is that both the parent classes may have different implementation for the same method. So they have different ways of doing the same thing. Now which implementation should the child class choose?
This leads to ambiguity in Multiple Inheritance. This is the main reason for Java not supporting Multiple Inheritance in implementation.
Lets say you have a class TV and another class AtomBomb. Both have method switchOn() but only TV has switchOff() method. If your class inherits from both these classes then you have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV. But you can implement multiple interfaces in Java.
In OOPS, what is meant by composition?
Composition is also known as “has-a” relationship. In composition,“has-a” relation relates two classes. E.g. Class Car has a steering wheel. If a class holds the instance of another class, then it is called composition
How aggregation and composition are different concepts?
In OOPS, Aggregation and Composition are the types of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are destroyed. E.g. A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of Steering Wheel.
In Aggregation, the relationship is weaker than Composition.E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books are also destroyed.
Books of a Library cannot exist without the Library. So Book and Library are related by Composition
Why there are no pointers in Java?
In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory locations. JVM is free to move the objects within VM memory. The absence of pointers helps Java in managing memory and garbage collection effectively. Also it provides developers with convenience of not getting worried about memory allocation and deallocation
If there are no pointers in Java, then why do we get NullPointerException?
In Java, the pointer equivalent is Object reference. When we use a it points to object reference. So JVM uses pointers but programmers only see object references.In case an object reference points to null object, and we try to access a method or member variable on it, then we get NullPointerException.
What is the purpose of ‘super’ keyword in java?
Is it possible to use this() and super() both in same constructor? ===> no
What is the meaning of object cloning in Java?
=======================Static=======================================
In Java, why do we use static variable?
Whenever we want to have a common property for all objects of a class, we use a class level variable i.e. a static variable.This variable is loaded in memory only once at the time of classloading. So it saves memory, since it is not defined per object in Java
Why it is not a good practice to create static variables in Java?
Static variables are common to all the objects of a class.
What is the purpose of static method in Java?
Java provides the feature of static method to create behavior at the class level. The static method is common to all the objects of a class. We do not need to create any object of a class to call a static method. So it provides convenience of not creating an object for
calling it.
Why do we mark main method as static in Java?
In what scenario do we use a static block?
At times, there is a class that has static member variables. These variables need some complicated initialization. At this time static block helps as a tool to initialize complex static member variable initialization.The static block is executed even before the execution of main.
Is it possible to execute a program without defining a main() method?
What happens when static modifier is not mentioned in the signature
of main method?
What is the difference between static method and instance method in
Java?
Often, there is a need to define a behavior for a class that is not dependent on member variables of an object. Such behavior is
captured in a static method. If there is a behavior dependent upon the member variables of an object, then we do not mark it static, it
remains as instance method.
To call as static method, we do not need to create an object. We just call it with class name. But to call an instance method, we need to
create/get an object first.
Instance member variables cannot be accessed by a static method. But an instance method can call both instance variables and static
variables
What is Runtime Polymorphism?
Runtime Polymorphism or Dynamic Polymorphism is the polymorphism that exists at runtime. In case of method overriding it
is not known which method will be called at runtime. Based on the type of object, JVM decides the exact method that should be called.
So at compile time it is not known which method will be called at run time.
Is it possible to achieve Runtime Polymorphism by data members in Java?
No. We need to create Runtime Polymorphism by implementing methods at two levels of inheritance in Java.
Explain the difference between static and dynamic binding?
In Static binding references are resolved at compile time. In Dynamic binding references are resolved at Run time.
E.g.
Person p = new Person();
p.walk(); // Java compiler resolves this binding at compile time.
public void walk(Object o){
((Person) o).walk(); // this is dynamic binding.
}
This is all about Most commonly asked Constructor interview questions in java.
Related Posts:
Inheritance Interview Question in java