OOPS Interview Questions and Answers for Beginners and Experienced


In this article, we will discuss important OOPS interview questions and answers for beginners and experienced. This article describes some advanced OOPS concepts so which will help experienced developers to refresh their OOPS knowledge.
Before we get started, check out this complete beginner to expert OOPS Tutorial in Java!
I would like to share my experience with OOPS interview questions. Let's list all commonly asked interview questions and answers regarding OOPS in Java.
  1. What is OOPS?
  2. What are Core OOPS Concepts?
  3. What is an Object?
  4. What is Class?
  5. What are the advantages of OOPS concepts?
  6. What is the difference between Procedural programming and OOPS?
  7. What is Abstraction and give real-world examples?
  8. What is Encapsulation and give real-world examples?
  9. What is Polymorphism?
  10. Explain different types of Polymorphism in Java
  11. What is Inheritance and give real-world examples?
  12. What is the difference between Abstraction and Encapsulation?
  13. What is multiple inheritance?
  14. What is the diamond problem in inheritance?
  15. Why Java does not support multiple inheritance?
  16. What is Static Binding and Dynamic Binding?
  17. What is Composition?
  18. What is Aggregation?
  19. What is Association?
  20. What is Cohesion?
  21. What is Coupling?
  22. What is Delegation?
  23. What is a SOLID OOPS Principle?
  24. What is the Single Responsibility Principle?
  25. What is the Open Closed Principle?
  26. What is the Liskov Substitution Principle?
  27. What is the Interface Segregation Principle?
  28. What is the Dependency Inversion Principle?
  29. What are All the Different Ways to Create an Object in Java?
Note that this article not only helps you to prepare for interviews but also help you to refresh your OOPS knowledge with simple definition and examples.

1. What is OOPS?

Object-Oriented Programming System is the programming technique to write programs based on the real world objects. The states and behaviors of an object are represented as the member variables and methods. In OOPS programming programs are organized around objects and data rather than actions and logic.

2. What are Core OOPS Concepts?

I would like to list basic and advance OOPS concepts here:

3. What is an Object?

In short, Object is an instance of a class. The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.

Real-world examples

  • Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
  • Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

4. What is Class?

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.
Following is a sample of a class.
public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

5. What are the advantages of OOPS concepts?

Major advantages of OOPS programming are;
  • Simplicity: OOPS programming objects model real-world objects, so the complexity is reduced and the program structure is clear.
  • Modularity: Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
  • Modifiability: It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
  • Extensibility: Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
  • Maintainability: Objects can be maintained separately, making locating and fixing problems easier.
  • Reusability: Objects can be reused in different programs.

6. What is the difference between Procedural programming and OOPS?

  • Procedural language is based on functions object-oriented language is based on real-world objects.
  • Procedural language gives importance on the sequence of function execution but object-oriented language gives importance on states and behaviors of the objects.
  • Procedural language exposes the data to the entire program but object-oriented language encapsulates the data.
  • Procedural language follows top-down programming paradigm but object-oriented language follows bottom-up programming paradigm.
  • Procedural language is complex in nature so it is difficult to modify, extend and maintain but an object-oriented language is less complex in nature so it is easier to modify, extend and maintain.
  • Procedural language provides less scope of code reuse but object-oriented language provides more scope of code reuse.

7. What is Abstraction and give real-world examples?

Definition

Abstraction means hiding lower-level details and exposing only the essential and relevant details to the users.

Real world example

  • A car abstracts the internal details and exposes to the driver only those details that are relevant to the interaction of the driver with the car.
  • For example phone call, we don't know the internal processing. In Java, we use abstract class and interface to achieve abstraction.
  • We never buy a "device", but always buy something more specific: iPhone, GSII, Nokia 3310 etc Here, iPhone, GSII, and N3310 are concrete things, the device is abstract.
Read more in-detail at Abstraction in Java with Examples.

8. What is Encapsulation and give real-world examples?

Definition

Encapsulation refers to combining data and associated functions as a single unit. In OOP, data and functions operating on that data are combined together to form a single unit, which is referred to as a class.

Real world example

  • Capsule, it is wrapped with different medicines.
  • A Java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
Read more about Encapsulation with a complete example at Encapsulation in Java with Example.

9. What is Polymorphism?

Polymorphism is the occurrence of something in various forms. Java supports various forms of polymorphism like polymorphic reference variables, polymorphic method, polymorphic return types, and polymorphic argument types.

Real life example of polymorphism

Suppose if you are in a classroom that time you behave like a student when you are in the market at that time you behave like a customer when you at your home at that time you behave like a son or daughter.
Read more about Polymorphism with a complete example at Polymorphism in Java with Example

10. Explain different types of Polymorphism in Java

  1. Compile time polymorphism or method overloading or static banding
  2. Runtime polymorphism or method overriding or dynamic binding

1. Compile time Polymorphism

If the class contains two or more methods having the same name and different arguments then it is method overloading.
The compiler will resolve the call to a correct method depending on the actual number and/or types of the passed parameters The advantage of method overloading is to increases the readability of the program.

2. Runtime Polymorphism

Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

11. What is Inheritance and give real-world examples?

Definition

Inheritance - IS-A relationship between a superclass and its subclasses. The process where one object acquires the members of another; plus can have its own.

Real world example

  • Dog (subclass) is-a of type Animal (superclass). So Dog can inherit (reuse) members of Animal class; plus it can have its own new behavior and properties.
  • In the Java library, you can see extensive use of inheritance. Below figure shows a partial inheritance hierarchy from a java.lang library. The Number class abstracts various numerical (reference) types such as Byte, Integer, Float, Double, Short, and BigDecimal.

12. What is the difference between Abstraction and Encapsulation?

  • Program to interfaces, not implementations” is the principle for Abstraction and “Encapsulate what varies” is the OO principle for Encapsulation.
  • Abstraction provides a general structure of a class and leaves the details for the implementers. Encapsulation is to create and define the permissions and restrictions of an object and its member variables and methods.
  • Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using four types of access level modifiers: public, protected, no modifier and private.

13. What is multiple inheritance?

A child class inheriting states and behaviors from multiple parent classes is known as multiple inheritance.

14. What is the diamond problem in inheritance?

In case of multiple inheritance, suppose class A has two subclasses B and C, and class D has two super classes B and C. If a method present in A is overridden by both B and C but not by D then from which class D will inherit that method B or C? This problem is known as the diamond problem.

15. Why Java does not support multiple inheritance?

Java was designed to be a simple language and multiple inheritance introduces complexities like the diamond problem. Inheriting states or behaviors from two different type of classes is a case which in reality very rare and it can be achieved easily through an object association.

16. What is Static Binding and Dynamic Binding?

Static or early binding is resolved at compile time. Method overloading is an example of static binding.
Dynamic or late or virtual binding is resolved at runtime. Method overriding is an example of dynamic binding.

17. What is Composition?

A Composition is an association represents a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. It has a stronger relationship.
Read more about Composition with a complete example at Composition in Java with Example.

18. What is Aggregation?

Aggregation is an association represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.
It is a specialized form of Association where all object has their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship.
Let’s take an example of the relationship between Department and Teacher. A Teacher may belong to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.

19. What is Association?

Association is a relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
Let’s take an example of the relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
Read more about Association with an example at Association in Java with Example.

20. What is Cohesion?

The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility.
Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system.

21. What is Coupling?

Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling. Low dependencies between “artifacts” (classes, modules, components).There shouldn’t be too much of dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.

22. What is Delegation?

Hand over the responsibility for a particular task to another class or method.
It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.
Read more about Delegation with an example at Delegation in Java with Example

23. What is a SOLID OOPS Principle?

SOLID is one of the most popular sets of design principles in object-oriented software development. It’s a mnemonic acronym for the following five design principles:
  • Single Responsibility Principle - A class should have only one reason to change
  • Open/Closed Principle - Software entities like classes, modules, and functions should be open for extension but closed for modifications.
  • Liskov Substitution Principle - Derived types must be completely substitutable for their base types.
  • Interface Segregation Principle - The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.
  • Dependency Inversion Principle - High-level modules should not depend on low-level modules. Both should depend on abstractions.

24. What is the Single Responsibility Principle?

"A class should have only one reason to change". Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. There should never be more than one reason for a class to change.
Read more about Single Responsibility Principle with an example at Single Responsibility Principle.

25. What is the Open Closed Principle?

Software entities like classes, modules, and functions should be open for extension but closed for modifications.
Read more about Open Closed Principle with an example at Open Closed Principle

26. What is the Liskov Substitution Principle?

Derived types must be completely substitutable for their base types.
Read more about Liskov Substitution Principle with an example at Liskov's Substitution Principle

27. What is the Interface Segregation Principle?

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.
Read more about Interface Segregation Principle with an example at  Interface Segregation Principle

28. What is the Dependency Inversion Principle?

Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module, but they should depend on abstractions.

29. What are All the Different Ways to Create an Object in Java?

There are 5 Different Ways to Create Objects in Java:
1. Using a new keyword
2. Using newInstance() method of Class class
3. Using newInstance() method of Constructor class 
4. Using Object Deserialization
5. Using Object Cloning – clone() method
Read more to see examples of each different ways to create an Object in Java at  What are All the Different Ways to Create an Object in Java?

Read more about Dependency Inversion Principle with an example at Dependency Inversion Principle.

Kindly suggest if you have other interview questions regarding OOPS in Java so that it will help others to prepare for Java Interviews.
Check out OOPS Tutorial in Java

Comments

  1. the hard part is polymorphism

    ReplyDelete
    Replies
    1. Check out http://www.javaguides.net/2018/08/polymorphism-in-java-with-example.html article which has described all about Polymorphism with it's types and examples.

      Delete

Post a Comment