OOPS Interview Questions and Answers for Beginners and Experienced - RameshMF/java-interview GitHub Wiki
In this article, we will discuss important OOPS interview questions and answers for beginners and experienced. This article describes some advanced OOPS concepts and which will help experienced developers to refresh thier OOPS knowledge.
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.
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.
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.
What OOPS Concepts?
I would like to list basic and advance OOPS concepts here:
- Object
- Class
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Composition
- Aggregation
- Association
- Cohesion
- Coupling
- Delegation
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).
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() {
}
}
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.
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.
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 at http://www.javaguides.net/2018/08/abstraction-in-java-with-example.html
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.
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.
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 java.lang library. The Number class abstracts various numerical (reference) types such as Byte, Integer, Float, Double, Short, and BigDecimal.
Diagram here
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.
What is multiple inheritance?
A child class inheriting states and behaviors from multiple parent classes is known as multiple inheritance.
Diagram here
What is the diamond problem in inheritance?
In case of multiple inheritance, suppose class A has two subclasses B and C, and a 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 diamond problem.
diagram here
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.
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.
What is Composition?
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.
diagram here
Read more at http://www.javaguides.net/2018/08/composition-in-java-with-example.html
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.
Diagram here
Read more at http://www.javaguides.net/2018/08/aggregation-in-java-with-example.html
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.
diagram here
Read more at http://www.javaguides.net/2018/08/association-in-java-with-example.html
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.
Read more at http://www.javaguides.net/2018/08/cohesion-in-java-with-example.html
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.
Read more at http://www.javaguides.net/2018/08/coupling-in-java-with-example.html
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.
What is 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 - High-level modules should not depend on low-level modules. Both should depend on abstractions.
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 http://www.javaguides.net/2018/02/single-responsibility-principle.html
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 http://www.javaguides.net/2018/02/open-closed-principle.html
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 http://www.javaguides.net/2018/02/liskov-substitution-principle.html
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 http://www.javaguides.net/2018/02/interface-segregation-principle.html
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.
Read more about Dependency Inversion Principle with an example at http://www.javaguides.net/2018/02/dependency-inversion-principle.html