OOPs Concepts in Java Interview Questions For Freshers & Experienced

OOPs Concepts in Java Interview Questions

Any Java interview is incomplete without Object-Oriented Programming concepts interview questions. Learning these concepts is the key to understanding how Java works. Therefore, to make your interview preparation smoother, we will be covering frequently asked OOPs concepts in Java questions and answers.

 

Although this article focuses on the core Object-Oriented Programming questions that are generally asked in entry-level Java interviews, experienced developers too can use this writeup to revisit the concepts.

 

We have divided the questions into the following categories for ease of reading:

 

OOPs Concepts in Java Interview Questions

  1. What is Object-Oriented Programming?
  2. What are the core OOPs concepts in Java?
  3. What are the advantages of Object-Oriented Programming features in Java?

 

Inheritance in Java Interview Questions

  1. What is Inheritance in Java?
  2. How many types of inheritance are there in Object-Oriented Programming? Which inheritance types does Java support?
  3. Why Multiple Inheritance is not supported in Java?
  4. Are there any limitations of Inheritance?
  5. What are static methods in Java? Can they be used in Inheritance?

 

Encapsulation in Java Interview Questions

  1. What is Encapsulation?
  2. What is Access Modifier in Java? What are the different types of Access Modifiers and their scope?
  3. What are Non-Access Modifiers in Java?

 

Abstraction in Java Interview Questions

  1. What is Abstraction in Java?
  2. What is the difference between Abstract class and Interface class?

 

OOPs Concepts in Java Interview Questions

 

1. What is Object-Oriented Programming?

It is a programming hypothesis that works on the principles of abstraction, inheritance, encapsulation, and polymorphism. The basic idea of Object-Oriented Programming is to produce objects, re-use them across the program, and utilize these objects to acquire results. Moreover, OOP relies on these objects, instead of functions, to bind data and operations together.


Explore the Job Openings in APAC

 

2. What are the core OOPs concepts in Java?

  • Class: A cluster of similar logical entities in a program. 
  • Object: It is an instance of a class. A class can have multiple instances.  
  • Inheritance: Inheritance refers to an Object-Oriented Programming concept in which one object acquires the characteristics of the object of another class.  
  • Polymorphism: The capability of a variable, object, or function to take on multiple forms.  
  • Abstraction: Abstraction in Java is a concept of representing vital features of a class or method without including their background information.  
  • Encapsulation: An OOPs concept in Java that packages data and code together. 

 

3. What are the advantages of Object-Oriented Programming features in Java?

The Java OOPs concepts offer various benefits that other procedural programming like C, Pascal, etc. do not offer.  

 

  • Data is encapsulated with procedures in the class to secure the data from unexpected modifications.
  • Java OOPs concepts encourage reusability and avoid redundancy by using the features of an existing class in a new class. For example, by using Multilevel Inheritance in Java, you can easily use features of class C, via class B into class A.
  • Objects use a message-passing technique for communication thereby creating the interface descriptions easier for external systems.
  • Reduces complexity through Inheritance which makes it the preferred approach for developing complex applications.
  • Uses a bottom-up approach that makes it easy to upgrade from small to large systems.
  • Code written using OOPs concepts in Java is easy to maintain.
  • Helps in building efficient applications with simpler development processes.

 

Inheritance in Java Interview Questions

 

4. What is Inheritance in Java?

Inheritance in Java is a process in which one class attains the features i.e. variables and procedures, of some other class.

 

In Object-Oriented Programming in Java, inheritance provides the concept of reusability of code within an application. Here, each subclass defines only the features that are unique to it, and the rest of the features it can acquire from the parent class. Therefore, inheritance creates a hierarchy of classes thereby making them easier to maintain.

 

Furthermore, the class which derives certain properties of another class is known as a subclass, derived class, or child class. Similarly, the class from whom the subclass inherits its properties is known as a superclass, base class, or parent class.

 

The subclass uses the keyword ‘extends’ to inherit a superclass. 

 

The syntax for implementing inheritance in Java:

 

class SuperClass { 
   ..... 
   ..... 
} 
class SubClass extends SuperClass { 
   ..... 
   ..... 
} 


Test your coding skills here

 

5. How many types of inheritance are there in Object-Oriented Programming? Which inheritance types does Java support?

There are six supported types of inheritance in OOPs, which are Single Inheritance, Multi-level Inheritance, Multiple Inheritance, Multipath Inheritance, Hierarchical Inheritance, and Hybrid Inheritance.

 

However, there are three types of inheritance Java supports:

 

  • Single Inheritance- A child class inherits a single parent class. For instance, ClassY inherits ClassX. 
  • Multilevel Inheritance- In Multilevel Inheritance in Java, a child class inherits a parent class that has inherited another class. For example, Class Z inherits ClassY. ClassY inherits ClassZ  
  • Hierarchical Inheritance- With this type of inheritance, Java enables multiple child classes to inherit a single parent class. i.e. ClassY and ClassZ inherit ClassX. 

 

6. Why Multiple Inheritance is not supported in Java?

Multiple Inheritance is a concept where a child class can inherit from more than one parent class. The reason why Multiple Inheritance is not supported in Java is that it can lead to ambiguity. For instance, if two or more parent classes that a child extends have a common method, then the compiler will be unable to decide which method to use.

 

Like multiple inheritance in Java, we cannot implement Hybrid Inheritance in Java too. As it is a combination of multiple and single inheritance, Hybrid Inheritance in Java implementation through classes will result in ambiguity as well.

 

Hence, to enforce simplicity, there is no provision of hybrid and Multiple Inheritance in Java.

 

7. Are there any limitations of Inheritance?

Inheritance in Java is a powerful concept to enable reusability. However, it too comes with a few limitations: 

 

  • Firstly, there is an increase in program execution time due to constant to and fro between parent and child classes. 
  • Next, Inheritance leads to tight coupling between the superclass and the subclass. 
  • Thirdly, in case of modifications in the program, the developer needs to make changes in both the superclass and the subclass. 
  • Lastly, it requires careful implementation otherwise, the program would yield incorrect functionality. 

 

8. What are static methods in Java? Can they be used in Inheritance?

In Java, static methods are created using the keyword ‘static’. As these methods are made static, they will be independent of any instances of the class. Also, static methods do not accept values from an instance variable of the object of the class. Instead, method parameters pass all the data to static methods. The computation then takes place on the values passed in the parameter, without any reference to the variables. 

 

Yes, we can inherit static methods in Java. A child class inherits all of the public and protected members of its parent class within the same package. Therefore, it will inherit static methods as well. Though the child class can use the inherited static methods as is, it cannot override them. 

 

OOPs concepts in Java Interview Questions: Encapsulation

 

9. What is Encapsulation?

Encapsulation in Java refers to wrapping up the data and the methods to access and transform the data into a single entity. If we talk about Encapsulation in Java with examples, the first name to appear will be of the Spring Bean class.

 

In encapsulation, the variables of a class will be hidden from other classes by declaring them as private. And to modify and view the values of these variables, we have to declare the setter and getter methods. Therefore, encapsulation is also known as data hiding.

 

As it is easier to understand Encapsulation in Java with examples, here is a simple program for the same:

 

/* File name : EncapTest.java */ 
/* Example taken from TutorialsPoint
 
public class EncapTest { 
   private String name; 
   private String idNum; 
   private int age; 
   public int getAge() { 
      return age; 
   } 
   public String getName() { 
      return name; 
   } 
   public String getIdNum() { 
      return idNum; 
   } 
   public void setAge( int newAge) { 
      age = newAge; 
   } 
   public void setName(String newName) { 
      name = newName; 
   } 
   public void setIdNum( String newId) { 
      idNum = newId; 
   } 
} 

 

10. What is Access Modifier in Java? What are the different types of Access Modifiers and their scope?

Access Modifiers in Java are specialized keywords with which a user can set the level of accessibility or scope for a class, method, constructor, or variables.

 

Java uses four types of access modifiers:

  • Private- Accessible only from within the class.  
  • Default- Can be accessed from within the package. No accessibility outside the package.  
  • Protected- Direct access from inside the package. You can use child class to access from outside the package. 
  • Public- Can be accessed from anywhere. 

 

The below image succinctly describes the scope of what is an Access Modifier in Java:

 

Different types of Access Modifiers in Java
Image credit: https://www.freecodecamp.org/
 

11. What are Non-Access Modifiers in Java?

These modifiers help to achieve a lot of miscellaneous programming functionalities in Java.

 

Overall there are six Non-Access Modifiers in Java:

  • Static Modifier: The Static Modifier can create variables and methods that are independent of any instance of a class.  
  • Final Modifier: A variable declared with the final keyword can be initialized only once. If you declare a variable static and final together, it becomes a constant in Java. Moreover, you cannot override a final method in Java and a final class can’t become a subclass. 
  • Abstract Modifier: Next is the Abstract Non-Access Modifiers in Javathat declares methods without any implementation. It's the subclass that inherits these methods and defines them. Similarly, you can’t instantiate an abstract class except through a subclass.
  • Synchronized Modifier: If we define a method using this modifier, then only a single thread will be able to access the synchronized method.
  • Transient Modifier: This modifier tells the Java Virtual Machine to skip a transient variable while serializing the object containing this variable. 
  • Volatile Modifier: The last Non-Access Modifier in Java is volatile that instructs JVM to ensure that the thread that is accessing the variable should merge its private copy with the master copy in the memory.


Explore the Job Openings in APAC

 

OOPs concepts in Java Interview Questions: Abstraction

 

12. What is Abstraction in Java?

In Object-oriented programming, the Abstraction definition says "it is a process of hiding the implementation details while providing only the functionality to the user. So the user will know only the essential details about what the object does but not how it does it. 

Moreover, the Abstraction Java concept is implemented through interfaces and abstract classes.

 

13. What is the difference between Abstract class and Interface class?

Abstract Class in Java

  • Can inherit either a single class or one abstract class at a time.
  • An Abstract Class in Java can extend another Java class.
  • Can have both abstract and concrete methods.
  • The keyword "abstract" is a must to declare a method as abstract.
  • Another difference between Abstract class and Interface class is that the Abstract class method in Java can be protected or public.
  • An Abstract Class in Java can have a static, final, or static final variable with any access specifier

 

Interface in Java

  • Can extend multiple interfaces at a time
  • Can only extend an interface
  • Can have abstract methods only.
  • The keyword "abstract" is optional for declaring an abstract method.
  • Interface, on the other hand, can use only public abstract methods.
  • An interface uses just public static final variables. Such variables are constants.

 

No, it is not possible to instantiate an Abstract Class in Java. This is because, in Abstraction, Java uses abstract classes that do not have a complete implementation. These classes only act as a template for child classes. To use an empty or partially empty structure of such a class, you need to extend it and define the abstract class methods in Java before using them.


Test your coding skills here

 

Other Backend Technology Interview Questions and Answers

C Programming Language Interview Questions | PHP Interview Questions | .NET Core Interview Questions | NumPy Interview Questions | API Interview Questions | FastAPI Python Web Framework | Java Exception Handling Interview Questions | Java Collections Interview Questions | System Design Interview Questions | Data Structure Concepts | Node.js Interview Questions | Django Interview Questions | React Interview Questions | Microservices Interview Questions | Key Backend Development Skills | Data Science Interview Questions | Python Interview Questions | Java Spring Framework Interview Questions | Spring Boot Interview Questions.

HACKERBUCK AWARDED