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.
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.
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;
}
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){...}
}
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
}
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.
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.
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.
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.
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.
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.
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.
Step 2 - Identify the core classes:
The core class can be identified as
Step 3 - Identify key methods and attributes in each class:
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.
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.