While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Welcome to OOPS interview questions and answers. There are many Object Oriented Programming languages such as Java, C++ and Python. Having a clear idea about OOPS concepts is very important if you are going to face any interview on these programming languages. That’s why I thought to share the top OOPS interview questions with you and provide detailed answers for them.
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.
Major advantages of OOPS programming are;
OOPS core concepts are;
Abstraction is an OOPS concept to construct the structure of the real world objects. During this construction only the general states and behaviors are taken and more specific states and behaviors are left aside for the implementers.
Encapsulation is an OOPS concept to create and define the permissions and restrictions of an object and its member variables and methods. A very simple example to explain the concept is to make the member variables of a class private and providing public getter and setter methods. Java provides four types of access level modifiers: public, protected, no modifier and private.
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.
A subclass can inherit the states and behaviors of it’s super class is known as inheritance.
A child class inheriting states and behaviors from multiple parent classes is known as multiple 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.
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.
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 run time. Method overriding is an example of dynamic binding.
"IS-A" relationship implies inheritance. A sub class object is said to have "IS-A" relationship with the super class 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 "instanceof" 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.
Association is a relationship between two objects with multiplicity.
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.
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.
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.
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.
A class is the specification or template of an object.
Object is instance of class.
If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.