Interviewinterview questionsTop 20 object oriented programming interview questions

Top 20 object oriented programming interview questions

In this article we are covering top object oriented programming interview questions, that help you to crack interview.

What are the main principles of Object-Oriented Programming?

Main principles of Object Oriented Programming (OOPS) are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

What is the difference between Object-Oriented Programming language and Object-Based Programming language?

Object Oriented Programming Languages such as Java and C++ adopt OPS definitions such as – Encapsulation, Abstraction, Polymorphism and Inheritance, etc.
Object Oriented Programming Languages adopt some of the features of OOPS but do not support Polymorphism and Inheritance. Egg. Egg. JavaScript, VBScript and so forth.
Object Oriented Programming Languages have support for Structures and you can create objects from a builder. They also endorse Encapsulation. They are also known as prototype-oriented languages.

In Java what is the default value of an object reference defined as an instance variable in an Object?

All references to an instance variable object in Java are null.

Why do we need constructor in Java?

Java is an object-oriented language in which objects are created and used. A constructor is a piece of code that is identical to a method. It is used to construct an entity and to set the initial state of the object.
A constructor is a special function that has the same name as the name of the class. There is no other way to construct an entity without a creator.
By default, Java provides the default constructor for each object. If we overload a constructor, we need to enforce the default constructor.

Why do we need default constructor in Java classes?

Default constructor is a no-argument constructor that is generated automatically by Java when no other constructor is defined.
The Java specification says it would include the default builder if there is no overloaded builder in the class. But it doesn’t mention much about the situation about which we write overloaded the builder in a class.

We need at least one constructor to build an object, which is why Java is the default constructor.

When we’ve overwhelmed the creator, Java assumes that we want some custom treatment in our code. Due to which the default constructor is not supported. But, as per the specification, it requires the default constructor. And there’s a mistake.

What is the value returned by Constructor in Java?

When we call a Java constructor, it returns an object created by it. This is how we build new objects in Java.

Can we inherit a Constructor?

No, Java does not support the constructor inheritance.

Why constructors cannot be final, static, or abstract in Java?

If we set the method as final, it means that we don’t want any class to bypass it. But the constructor (as per the Java Language Specification) cannot be overridden. There is no point, however, in marking it final.
If we describe a method as abstract, it implies that it has nobody and that it should be enforced in a child class. But the constructor is implicitly renamed while the current keyword is used. It wants a body, therefore.
If we set a method as static, it implies that it belongs to a class, but not to a specific object. The builder is often called to initialize an entity. There is also little use of static constructor marking.

What is the purpose of ‘this’ keyword in java?

  • In Java, ‘this’ keyword refers to the current instance of the object.
  • It is useful for differentiating between instance variables and local variables.
  • It can be used to call constructors. Or it can be used to refer to the instance.
  • In the case of method overriding, this is used for falling the method of the current class.

Explain the concept of Inheritance?

Inheritance is an important principle for object-oriented programming. Any artifacts have certain features and actions in common. By using Inheritance, we can place typical actions and characteristics in a base class that is also known as a superclass. And then all objects with a similar behavior inherit this base set.
The IS-A relationship is also represented. Inheritance supports, re-uses coding, method overriding, and polymorphism

Which class in Java is the superclass of every other class?

Java is an object oriented programming language. In Java, Object class is the superclass of every other class.

Why Java does not support multiple inheritance?

Multiple Inheritance means that a class may inherit actions from two or more parent classes. The problem with Multiple Inheritance is that all parent classes might have separate implementations for the same process. And they’ve got different ways to do the same thing. Now, what kind of implementation does the child class choose?
In Multiple Inheritance, which leads to complexity. This is the main reason why Java does not support the implementation of Multiple Inheritance.

In OOPS, what is meant by composition?

The composition is also known as the “has-a” relationship. In composition, the “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, the types of association relations are aggregation and composition. A composition is a very close friendship. When the composite object is destroyed, all its pieces will be destroyed. E.g. Yes. The car’s got a steering wheel. If the Automobile entity is lost, there is no sense to the Steering Wheel.
In Aggregation, the relationship is weaker than in Composition. For example, the Library has students. If the Library is lost, there are still students. So the Library and the Student are linked to the Aggregation. There are books in the library. When the Library is demolished, the Books will therefore be destroyed. The Library Books cannot exist without the Library. So Book and Library are related to Composition.

What is the purpose of the ‘super’ keyword in java?

The ‘super‘ keyword is used in the child class method or constructor. This applies to the immediate parent class of the object.
By using ‘super‘ we will call the parent class method from the child class method.
We can also call the parent class constructor from the kid class constructor by using the ‘super‘ keyword.

Is it possible to use this() and super() both in same constructor?

No, Java does not allow both super() and this() to be used in the same constructor. As per the Java specification, the super() or this() must be the first declaration in the constructor.

What is the meaning of object cloning in Java?

The Object.clone() method is used to build an identical replica of an object in Java. It’s behaving like a copy constructer. Creates and returns a replica of an object of the same class and with all fields containing the same values as the original object.
One of the drawbacks of cloning is that a return type is an object. It needs to be specifically allocated to the actual type.

In Java, why do we use static variable?

Whenever we wish to use common property for all objects in a class, we use a class-level variable, i.e. a static variable.
This variable is loaded into memory only once at the time of class initialization. But it saves memory since it’s not specified by a Java object.

Why it is not a good practice to create static variables in Java?

Static variables are common to all the objects of a class. If a new object is created, there is no need to test the value of static variable. Any code that uses a static variable can be in any state. It can be within a new object or at a class level. So the scope of static variable is open-ended in a Java class.
If we want tighter control on scope, then variables should be created at the object creation level.
Also defining static variables is not a good practice because they go against the principles of Object-Oriented Programming

What is the purpose of static method in Java?

Java offers the static method feature to build class-level level. The static method is common to all class objects. We don’t need to create a class object to call a static function. But it allows you the ease of not having an entity to name it.
The static method can also be used to access and change static data participants. This also serves to keep the actions as well as the state at the class level.


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today


Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article