Java interview – OOP

1.What is Object-oriented Programming?

Object-oriented programming is a programming model or approach where the programs you create are organized around objects rather than logic and functions. The states and behaviors of an object are represented as member variables and methods. This approach is ideal for the programs that are large and complex and need to be actively updated or maintained. Here below is an example of OOP where this little piece is focused on Person. There can be other entities like payment method, Account, etc. Programs are organized into entities.

2. What are the advantages of OOPs?

Simplicity: Domain models or entities are modeled after real-world objects. It is easier to understand and to relate to. The program structure is also easier to comprehend.

Modularity: Each object is decoupled from other objects of the system.

Modifiability: It is easy to make minor changes in the data or the functionalities 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.

3. What is the difference between procedural programming and OOPs?

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

4. What are the core concepts of OOPs?

  1. Inheritance: Inheritance is a process where one class acquires the properties and functionalities of another.
  2. EncapsulationEncapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
  3. AbstractionAbstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
  4. PolymorphismPolymorphism is the ability of a variable, function or object to take multiple forms.
  5. Composition
  6. Association
  7. Aggregation

5. What is the difference between Abstraction and Encapsulation?

  1. “Program to interfaces, not implementations” is the principle for Abstraction and “Encapsulate what varies” is the OO principle for Encapsulation.
  2. 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.
  3. 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.

6. What is the diamond problem in inheritance?

In the case of multiple inheritances, suppose class A has two subclasses B and C, and class D has two superclasses 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.

7. Why Java does not support multiple inheritances?

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

8. What is Static Binding and Dynamic Binding?

Static or early binding is resolved at compile time. Method overloading is an example of static binding. Static binding is faster than dynamic binding.

Dynamic or late or virtual binding is resolved at run time. Method overriding is an example of dynamic binding. This decision making during runtime makes Dynamic binding slower than static binding.

9. What is the meaning of the “IS-A” and “HAS-A” relationship?

“IS-A” relationship implies inheritance. A subclass object is said to have an “IS-A” relationship with the superclass or interface. If class A extends B then A “IS-A” B. It is transitive, that is, if class A extends B and class B extends C then A “IS-A” C. The “instance of” operator in java determines the “IS-A” relationship.

When a class A has a member reference variable of type B then A “HAS-A” B. It is also known as Aggregation.

10. What is an Association?

Association is a relationship between two objects with multiplicity.

11. What is Aggregation?

Aggregation is also known as “HAS-A” relationship. When class Car has a member reference variable of type Wheel then the relationship between the classes Car and Wheel is known as Aggregation. Aggregation can be understood as “whole to its parts” relationship.

Car is the whole and wheel is part. The wheel can exist without the Car. Aggregation is a weak association.

12. What is Composition?

Composition is a special form of Aggregation where the part cannot exist without the whole. Composition is a strong Association. Composition relationship is represented like aggregation with one difference that the diamond shape is filled.

13. What is Dependency?

When one class depends on another because it uses that class at some point in time then this relationship is known as Dependency. One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class. A Dependency is drawn as a dotted line from the dependent class to the independent class with an open arrowhead pointing to the independent class.

14. What is the difference between Association and Dependency?

The main difference between Association and Dependency is in case of Association one class has an attribute or member variable of the other class type but in case of Dependency a method takes an argument of the other class type or a method has a local variable of the other class type.

Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *