Java - Object Oriented Programming - Interview Questions

Java is an object-oriented programming language. Hence questions regarding the object oriented principles of Java programming language are commonly asked in interviews - for beginner as well as for senior level positions; and for all job roles including Java programmers, designers, architects and managers.

Questions on Java object oriented programming principles are commonly asked in telephonic screening interviews as well. Good understanding of Java objected oriented principles is important in interviews for senior level designer and architect roles, where you will be asked to design solutions based on real world scenarios. These are white boarding design questions such as 'Design a parking structure' or ' Design a payroll system' etc.

Below Java object oriented programming interview questions, answers, coding samples and tips will help you get grounded on the fundamentals of Java object oriented programming, which is sufficient for most Java programming interviews. In addition strategies for approaching and solving the scenario based design questions are also provided below.

Important Keywords are listed at the end of the questions. Review them, make them a part of Java vocabulary, and talk about them confidently during your interview process.

What is object-oriented programming? What are the key principles of object-oriented programming?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Amazon, American Airlines, Charles Schwab, FedEx, Geico, Honeywell, Intel, J.P Morgan, L3 Technologies, Microsoft, New York Life, Northrop Grumman, Oracle, Paycom, State Farm, Ultimate Software, Veritas, Workday,

Object-oriented programming is a programming methodology based on the concept of 'objects' which contain data, in the form of fields, also called as attributes; and code, in the form of procedures, also called as methods or functions.

Java is a class based object-oriented programming language, which means objects in Java are instances of classes. Think of a class as a blueprint, and object as an instance of this blueprint.

Object-oriented programming is based on 4 key principles - Abstraction, Encapsulation, Inheritance and Polymorphism.

What is Abstraction?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Accenture, Allscripts, C.H. Robinson, Deloitte, FDM Group, Guidewire, Infosys, Microsoft, Revature, Sphere Solutions, TIBCO Software, Tribro,

Abstraction is one of the four key principles of object-oriented programming.

Abstraction is the principle of object-oriented programming in which unimportant characteristics of an object are refined, removed or hidden; and only the key set of characteristics are represented to the outside world.

In other words, Abstraction is the principle of perceiving real life things from a particular perspective, based on the system we are designing; focusing only on those aspects that are relevant to this system and ignoring all other aspects.

For Example - If we are modeling a person in a payroll system, we would capture and focus on the name, age, title, company and salary attributes. We would ignore other attributes such as weight, height etc. which are not relevant in this context

Whereas, if we are modeling a person in a hospital patient system, we would capture and focus on the name, age, height, weight etc. We would ignore attributes such as company and salary of the person.

//Model of person in a payroll system
public class person {
private String name;
private int age;
private String companyName;
private double salary;
}
//Model of person in a hospital patient system
public class person {
private String name;
private int age;
private float height;
private float weight;
}

What is Encapsulation?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Accenture, Amazon, Apple, Atos, Avande, Bloomberg LP, Digiweb, Infosys, Microsoft, OSISoft, Panasonic, Pariveda, Paycom, Raytheon, Revature, Shure, Tallan, TCS, Thermo Fisher Scientific, Wipro, Zappo,

Encapsulation is one of the four key principles of object-oriented programming

Encapsulation is the principle of object-oriented programming in which methods, and the data that those methods act on, are bundled together into the same component. In Java programming language, related methods and fields are bundled together in Classes, instances of which are Objects.

Encapsulation enables data hiding in Java. i.e. the data in one object can be hidden from other objects. Methods act as intermediaries that access and modify the data within its containing object. Objects can access or manage data of other objects by calling methods of the other object.

Example:
public class person {
//private - name is hidden from other classes
private String name;
//private - age is hidden from other classes
private int age
//private - companyName is hidden from other classes
private String companyName;
//private - salary is hidden from other classes
private double salary;

//Setters and getters to access and modify companyName
public String getCompanyName(){...}
public String setCompanyName(String companyName){...}
}

What is Inheritance?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Absolute, Accenture, Antra, BetterCloud, Bloomberg LP, CGI, Continental, CSX, Deloitte, Fidelity Investments, General Atomics, Goldman Sachs, Kronos Incorporated, Lockheed Martin, Maverick Software, Morgan Stanley, Norfolk Southern, Northrop Grumman, Raytheon, Solstice, USAA,

Inheritance is one of the four key principles of object-oriented programming

Inheritance is the principle of object-oriented programming in which one or more classes can inherit their fields and methods from another class. The class whose fields and methods are inherited by other classes is called the super class or parent class. The class (or classes) inheriting the state and functionality of the super class or parent class is called the child class.

In Java programming language, the keyword 'extends' is used to specify that a class is a child of another class.

In the following example, the class Car extends from the class Vehicle. The class Car inherits the fields and methods of the class Vehicle. The class Car is the child class, the class Vehicle is the parent class.

This relationship in object-oriented programming language is commonly termed as 'Is-A' relationship. In this example, we can say that a Car Is-A Vehicle.

//Parent Class - Vehicle
public class Vehicle {
public double speed;
public void start(){...}
public void stop(){...};
}

//Child Class - Car
public class Car extends Vehicle{
//Inherits fields and methods of class Vehicle
}

What are Interfaces in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Accenture, Addteq, Amazon, Cisco Systems, Expedia, General Motors, J.P. Morgan, Majesco, Morgan Stanley, OrderDynamics, Revature, Sapient, Stingray Digital, Tuenti, VMWare, Yahoo,

Interfaces are reference types in Java programming language. Interfaces declares methods, i.e. behaviors, but do not implement them. A class inherits the methods of an interface and implements the methods...

*** Complete answer and code snippet in the Java Interview Guide.

What is Polymorphism?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Amazon, BAE Systems, Barclays Capital, CGI, Credit Suisse, FDM Group, Goldman Sachs, Hamilton, Harris, Honeywell, IBM, ICF, Infosys, J.P. Morgan, Micron, Microsoft, Nokia, Northrop Grumman, Paychex, SAP, Sears, Vanguard, Viasat, Vyom Labs, Yahoo,

Polymorphism is one of the four key principles of object oriented programming.

Polymorphism, which means 'having multiple forms' in Greek, is the principle of object oriented programming in which an object can have multiple forms. In Java programming language polymorphism is enabled by the use of inheritance and interface types. Polymorphism is in effect whenever we use a parent class reference or interface type to refer to a child object...

*** See complete answer and code snippet in the Java Interview Guide.

What is generalization. What are the two forms of generalizations?

 Key Concept
* Similar Java Interview Question Recently Asked @ Saama Technologies,

Generalization is the concept of object-oriented programming in which common properties and behaviors of two or more types can be defined in a single type; which can be inherited and reused...

*** See complete answer in the Java Interview Guide.

What is the difference between aggregation and composition?

 Key Concept
* Similar Java Interview Question Recently Asked @ PTC, TechnoGen, Virtusa,

Aggregation is a relationship between classes, in which one class is a part of another class. This relationship is also called as part-of whole relationship. In aggregation, the part can exist independently of the whole. An example of aggregation is car and engine. Engine is part of a car, but can exist without the car.

*** See complete answer in the Java Interview Guide.

What are SOLID principles of Object Oriented Programming?

 FAQ
* Similar Java Interview Question Recently Asked @ Citi, Dell, Honeywell, Luxoft, OnTrac, Paycom, Qualcomm, Schroders, Spryker, Target,

SOLID is an acronym for five Object Oriented Design principles that make software designs more understandable, flexible and maintainable. The SOLID acronym was introduced by Michael Feathers. Following are the five SOLID principles...

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

Approach to white board design questions

 

For senior roles, specially for designer and architect roles, you will most likely be asked to design a real world system using object-oriented principles. This involves white boarding the design with UML diagrams, usually the class diagram, followed by implementation code using appropriate data structures, efficient algorithms and design patterns. Your solution will be evaluated for application of object-oriented principles, application of design patterns, appropriate use of data structures, use of efficient algorithms etc.

The interviewer typically starts off with a simple question such as 'design an elevator system using object-oriented principles' or 'design a parking lot using object-oriented principles'. You will usually be given half hour to one hour to design the solution. The interviewer will evaluate how you approach the problem and design your solution.

The following six step approach usually helps in solving these type of design questions.

1. Confirm the scope and assumptions with interviewer.

2. Identify the core classes.

3. Identify key methods and attributes in each class.

4. Identify the relationships between the classes utilizing OOD and design patterns.

5.Summarize the design using design diagrams.

6. Develop the code using appropriate data structures and algorithms.

Below questions and high level answers will highlight this six step approach.

Design a parking lot using object oriented principles.

 FAQ

We can tackle the problem using the six step approach as follows.

Step 1 - Confirm the scope and assumptions with interviewer:
Following are some of the questions you would want to ask the interviewer.

  • How many levels does the parking lot have?
  • How many parking spots are there in each parking level?
  • Are there different kinds of parking spots like compact, regular etc?
  • What kind of vehicles are allowed to park in the parking lot?
  • Is there a 'parking full' indicator for the parking structure?
  • Is there a 'parking full' indicator at the parking level?

Let's assume that you have to design for a parking structure with 3 levels, each level containing 200 parking spots, all spots are regular spots, cars only can park in this parking lot, and it has 'parking full' indicators both for the parking lot as well as for each level

Step 2 - Identify the core classes:
The core class can be identified as

  • ParkingLot
  • ParkingLevel
  • ParkingSpot
  • Vehicle

Step 3 - Identify key methods and attributes in each class:

  • ParkingLot - Contains one or more ParkingLevels (Composition/Has-A relationship) and has methods isFull(), park(), unPark().
  • ParkingLevel - Contains ParkingSpots (Composition/Has-A relationship) and has methods isFull(), park() and unPark().
  • ParkingSpot - Can have zero or one vehicle parked in it (Aggregation) and has method isOccupied().
  • Vehicle - Has unique vehicle id and parking spot id, and has methods park() and unPark().

Step 4 - Refine using OOD and design patterns:
Remove business logic from domain objects and add them to new business objects. Eliminate ParkingSpot class and instead maintain list of spot id and vehicle ids in the PrakingLevel class.

  • ParkingLot - Contains one or more ParkingLevels.
  • ParkingLotBO - Has methods isFull(), park() and unPark().
  • ParkingLevel - Contains map of spot ids to vehicle ids.
  • ParkingLevelBO - Has methods isFull(), park() and unPark().
  • Vehicle - Has methods park() and unPark().
  • ParkingLotController - Acts as a gateway to the parking lot.

Step 5 - Summarize the design using design diagrams:
Based on information from above steps draw a high level design diagram. Usually a UML class diagram is expected by the interviewer.

Step 6 - Develop the code using appropriate data structures and algorithms.

The Java Interview Guide lists out the above steps in detail and has the complete code for this design question.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example