OOPS Interview Questions And Answers List in 2020

Today , in this article, we will go through the comprehensive list of top oops interview questions and answers in Java.

Current article is part of our series on Java Interview Questions.

As OOPs is one of the most fundamental concept of Java language, it is a very high probability that interview will ask questions on this. It doesn’t matter if you are fresher or developer with many years of experience.

That’s why we have included questions from the very basic to the very tricky. So, Let’s start.

oops interview questions and answers

OOPS Interview Questions And Answers

1. What is OOPS?

OOPS means object oriented programming. It is a programming paradigm in which application works by interaction between different objects.

All an application does is:

  1. Create different types of objects.
  2. Enable them to interact in certain way to achieve the desired result.

2. What do you mean by an Object?

An object in Java is the “instance” of a Java Class.

3. What is a Class then?

A Class is similar to high level data type typically representing a real-world entity.

In Java it is a combination of instance variables, static variables, instance methods and static methods.

It works as a template for object creation.

3. Could you give me a real-world example of a Class and an Object?

Sure. Let’s say in a banking application we have to manage different accounts for different users. So, we can create:

  1. One Class representing “BankAccount”
  2. And another class for “BankUser”

Subsequently, we can create hundreds of different user objects from the same BankUser class and similarly Account objects from BankAccount class.

4. What are the basic concepts of OOPS?

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
oops basic concepts and features

5. What is Abstraction?

Abstraction means “Hiding Complexity And Showing Features”.

Abstraction is a concept which enables application designers and developers to hide out smaller and non-essential details.

6. What is Encapsulation?

Encapsulation is the technique by which we combine the data/variables with the code which manipulates it, in a single unit.

In Java, this unit is a method.

7. What is the difference between Abstraction vs Encapsulation?

Abstraction is purely a concept which can be used for different things at different level.

For example: We can abstract a whole application by exposing it’s API to the client application.

In MVC pattern, Controller works as abstraction between Model and View.

We can apply the same concept even at the level of Class and methods.

In this manner we can say that Encapsulation is just one form of Abstraction in which we are abstracting the “manipulation of data”.

8. What are access modifiers?

“Access modifiers” is the Java language feature which controls the visibility of a Java component like Class, method, variables.

Java provides different keywords to define different visibility scopes.

9. What are different access-modifiers in Java?

ModifierScope
private Within the class
default Within the package
protected Within the package + sub-classes in other packages
public open to all

10. Is using access-modifiers Encapsulation?

No. Encapsulation is binding of data with code which works on that data. That’s it.

But, access-modifiers helps bring stronger encapsulation.With the help of access-modifiers, we can restrict the visibility of data members so that it is accessible only through our encapsulated method.

In this way direct access of data member, which is breaking of encapsulation, is stopped.

11. What is “abstract” keyword in Java?

With the help of abstract keyword, we can enable the abstraction at Class and/or method level in Java.

12. What is Inheritance?

It’s an OOPS feature by which a subclass can inherit the state and behavior of it’s parent class by inheriting class level variables and methods.

13. How can you implement inheritance in Java?

By using “extends” keywords on a Class.

14. Does Java supports multiple inheritance?

No.

15. Why Java doesn’t supports multiple inheritance?

Multiple inheritance leads to diamond problem. To avoid that altogether, Java did not support multiple inheritance.

16. What is Polymorphism?

Polymorphism means “many forms”.

Basically with polymorphic feature, a method behaves differently in different context.

17. What are different types of Polymorphism?

  1. Compile-time
  2. Run-time

18. What is compile-time Polymorphism? Give an example.

In compile-time Polymorphism (also known as static binding), linking of which logic will get executed is done at the time of program compilation phase.

Example: Method Overloading.

19. What is method overloading?

Method overloading is the ability to have multiple methods with the same name given type or number of arguments to the methods are different.

For example:

int add(int a, int b) {
return a+b;
}

int add(int a, int b, int c) {
return a+b+c;
}

We just need to remember “add” method and provide the available arguments and it should work.

Which method will get executed is already decided at the compilation time. Hence, it is also called as static binding.

20. What is runtime Polymorphism? Give an example.

In run-time Polymorphism (also known as dynamic binding), linking of which logic will get executed is done at the time when application is actually running.

Example: Method Overriding.

21. What is method overriding?

Method overriding is the ability to define method with the same name and same arguments in parent and child class.

For example:

//A.java
class A {
int act(int a, int b) {
return a+b; //addition
}
//A.java
class B extends A {
int act(int a, int b) {
return a*b; // multiplication
}

BUT, which method will get executed is decided at the run-time. This is why it is called as dynamic binding.

22. What are different rules for method overriding?

These are the rules to override a parent method in a subclass:

  1. Parent class’s method should be visible in subclass by having appropriate access-modifier meaning:
    1. Parent class method should not be private or final.
    2. If subclass is in other package, then parent class’s method should be either protected or public.
  2. Method name should be exactly same.
  3. Number of arguments must be exactly the same.
  4. Type of arguments must be exactly the same.
  5. Access-modifier can not be more restrictive (private < default < protected < public).So, if parent method is protected, a subclass method can be either protected or public.
  6. No restriction on unchecked exceptions.
  7. Checked exceptions needs to be either same or more restrictive BUT NOT newer or broader.
  8. Return type can be either same or subclass of parent’s return type.
  9. Overriding is not applicable for static methods.

With this we are finished with oops interview questions and answers, both general concept of oops as well as specific to Java.

Leave a Comment