JAVA – OOPS INTERVIEW QUESTION ANSWER
1. What is Abstraction in OOP?
Abstraction is a process of hiding the implementation details from the user. only the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces It helps to reduce programming complexity and effort.
For More Detailed Information: Click Here
2. Rules of Abstract Method
- Abstract methods do not have an implementation; it only has method signature
- If a class is using an abstract method they must be declared abstract. The opposite cannot be true. This means that an abstract class does not necessarily have an abstract method.
- If a regular class extends an abstract class, then that class must implement all the abstract methods of the abstract parent
3. Difference between Encapsulation and Abstract?
- Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding).
- While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing the interface to the user and hiding the details of implementation.
4. What is the advantages of Abstraction?
- It reduces the complexity of viewing the things.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only important details are provided to the user.
5. What is Encapsulation?
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation.
Capsule
6. What is polymorphism?
When one task is performed in different ways i.e. known as polymorphism For example: to convince the customer differently, to draw something e.g: shape or rectangle etc. In java we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
7. What is Inheritance?
When one object acquires all the properties and behaviors of parent object i.e. known as inheritance. It provides code reusability. It’s used to achieve runtime polymorphism.
8. Why Java does not support multiple inheritance?
Java was designed to be a simple language and multiple inheritance introduces complexities like 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.
9. What is IS-A Relationship?
- In Java, an Is-A relationship depends on inheritance. Further inheritance is of two types, class inheritance and interface inheritance.
- It is used for code reusability in Java. For example, a Potato is a vegetable, a Bus is a vehicle, a Bulb is an electronic device and so on.
- One of the properties of inheritance is that inheritance is unidirectional in nature. Like we can say that a house is a building. But not all buildings are houses.
- We can easily determine an Is-A relationship in Java. When there is an extends or implement keyword in the class declaration in Java, then the specific class is said to be following the Is-A relationship.
For Ex: Class Bike—-–IS-A——->Class Pulsar——–HAS-A-——>Class Engine
10. What is HAS-A Relationship?
- In Java, a Has-A relationship is also known as composition. It is also used for code reusability in Java.
- In Java, a Has-A relationship simply means that an instance of one class has a reference to an instance of another class or an other instance of the same class.
- For example, a car has an engine, a dog has a tail and so on. In Java, there is no such keyword that implements a Has-A relationship. But we mostly use new keywords to implement a Has-A relationship in Java.
For Ex: Class Bike—-–IS-A——->Class Pulsar——–HAS-A-——>Class Engine
11. What is Association?
Association is a relationship between two objects with multiplicity.
12. 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. Wheel can exist without the Car. Aggregation is a weak association.
13. 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.
What is Dependency?When one class depends on another because it uses that 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 are the advantages of OOPS concepts?
- 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.
15. What are the core concepts of OOPS?
OOPS core concepts are;
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Composition
- Association
- Aggregation