OOPS Concept Interview Questions

What are different oops concept in java?

OOPs stands for Object Oriented Programming. The concepts in oops are similar to any other programming languages. Basically, it is program agnostic. The different OOps concepts are :

  • Polymorphism

  • Inheritance

  • Abstraction

  • Encapsulation

  • Aggregation

  • Composition

  • Association

Learn Java in Detail

What is polymorphism?

The ability to define a function in multiple forms is called Polymorphism. In java, c++ there are two types of polymorphism: Compile time polymorphism (overloading) and Runtime polymorphism (overriding).

Mehtod overriding : Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.

Overloading : Overloading is determined at the compile time. It occurs when several methods have same names with:

  • Different method signature and different number or type of parameters.

  • Same method signature but different number of parameters.

  • Same method signature and same number of parameters but of different type.

Example of Overloading :

What is inheritance?

Inheritance allows a Child class to inherit properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

What is multiple inheritance and does java support?

If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes, to overcome this problem it allows to implement multiple Interfaces.

What is an abstraction ?

Abstraction is a way of converting real world objects in terms of class. Its a concept of defining an idea in terms of classes or interface. For example creating a class Vehicle and injecting properties into it. E.g

What is Encapsulation?

The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how things work and just exposing the requests a user can do.

What is Association?

Association is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

What is Aggregation?

Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about "has-a" relationship.

What is Composition ?

Composition is again specialize form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.