JAVA OOP INTERVIEW QUESTIONS
Differentiate between data abstraction and encapsulation.
Data abstraction Solves the problem at the design level Allows showing important aspects while hiding implementation details Encapsulation Solves the problem at the implementation level Binds code and data together into a single unit and hides it from the world
Can you create an instance of an abstract class?
No. Instances of an abstract class cannot be created because it does not have a complete implementation. However, instances of subclass inheriting the abstract class can be created.
What is a superclass? What is a subclass?
A superclass or base class is a class that acts as a parent to some other class or classes. For example, the Vehicle class is a superclass of class Car. A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a derived of Vehicle class.
What is an object?
An object is a real-world entity that is the basic unit of OOPs for example chair, cat, dog, etc. Different objects have different states or attributes, and behaviors.
What is the difference between a class and a structure?
Class: User-defined blueprint from which objects are created. It consists of methods or set of instructions that are to be performed on the objects. Structure: A structure is basically a user-defined collection of variables which are of different data types.
What is method overloading?
Method overloading is a feature of OOPs which makes it possible to give the same name to more than one methods within a class if the arguments passed differ.
What is Object Oriented Programming?
Programming(OOPs) is a type of programming that is based on objects rather than just functions and procedures and enables code-reuse. Individual objects are grouped into classes. OOPs implements real-world entities like inheritance, polymorphism, hiding, etc into programming. It also allows binding data and code together.
What is static polymorphism?
Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of compile-time polymorphism is method overloading.
Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if: It is a static method The base class is inherited by some other subclass
What is a class?
A class is a prototype that consists of objects in different states and with different behaviors. It has a number of methods that are common the objects present within that class.
What are 'access specifiers'?
Access specifiers or access modifiers are keywords that determine the accessibility of methods, classes, etc in OOPs. These access specifiers allow the implementation of encapsulation. The most common access specifiers are public, private and protected. However, there are a few more which are specific to the programming languages.
What is an abstract class?
An abstract class is a class that consists of abstract methods. These methods are basically declared but not defined. If these methods are to be used in some subclass, they need to be exclusively defined in the subclass.
How to achieve data abstraction?
Data abstraction can be achieved through: Abstract class Abstract method
What is data abstraction?
Data abstraction is a very important feature of OOPs that allows displaying only the important information and hiding the implementation details. For example, while riding a bike, you know that if you raise the accelerator, the speed will increase, but you don't know how it actually happens. This is data abstraction as the implementation details are hidden from the rider.
What is encapsulation?
Encapsulation refers to binding the data and the code that works on that together in a single unit. For example, a class. Encapsulation also allows data-hiding as the data specified in one class is hidden from other classes.
What is hybrid inheritance? What is hierarchical inheritance?
Hybrid inheritance is a combination of multiple and multi-level inheritance. Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example, the vehicle class can have 'car', 'bike', etc as its subclasses.
What are the limitations of inheritance?
Increases the time and effort required to execute a program as it requires jumping back and forth between different classes The parent class and the child class get tightly coupled Any modifications to the program would require changes both in the parent as well as the child class Needs careful implementation else would lead to incorrect results
What are the main features of OOPs?
Inheritance Encapsulation Polymorphism Data Abstraction
What is inheritance?
Inheritance is a feature of OOPs which allows classes inherit common properties from other classes. For example, if there is a class such as 'vehicle', other classes like 'car', 'bike', etc can inherit common properties from the vehicle class. This property helps you get rid of redundant code thereby reducing the overall size of the code. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.
What is method overriding?
Method overriding is a feature of OOPs by which the child class or the subclass can redefine methods present in the base class or parent class. Here, the method that is overridden has the same name as well as the signature meaning the arguments passed and the return type.
What is the difference between multiple and multilevel inheritance?
Multiple inheritance comes into picture when a class inherits more than one base class.Example: A class defining a child inherits from two base classes Mother and Father. Multilevel inheritance means a class inherits from another class which itself is a subclass of some other base class. Example: A class describing a sports car will inherit from a base class Car which inturn inherits another class Vehicle
What is the difference between public, private and protected access modifiers?
Name. Accessibility from own class Public. Yes Private Yes Protected Yes Accessibility from derived class Yes No Yes Accessibility from world Yes No No
What is operator overloading?
Operator overloading refers to implementing operators using user-defined types based on the arguments passed along with it.
Differentiate between overloading and overriding.
Overloading Two or more methods having the same name but different parameters or signature Resolved during compile-time. Overriding Child class redefining methods present in the base class with the same parameters/ signature Resolved during runtime
What is dynamic polymorphism?
Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism which is resolved during runtime. An example of runtime polymorphism is method overriding.
What are the different types of inheritance?
Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid inheritance