Java Interview questions and answers
1. What is Object Oriented Programming?
Object-Oriented Programming(OOPs) is a kind of programming which is based on objects instead of functions and procedures. OOPs follow or implements real-world entities like polymorphism ,inheritance, hiding, etc into programming strategies.
2. Why should we use OOPs?
- Oops allow use reusability of Code means we can be reused code with the help of inheritance hence reducing redundancy.
- Oops allow to bind data and code together by encapsulation.
- OOPs allows data privacy, hence, private data is kept confidential.
- Oops allow use to divide Problems into different parts to making it simple to solve.
- In oops the concept of polymorphism gives flexibility to the program by allowing the entities to have multiple forms.
3. What are the main features of OOPs?
- Data Abstraction
4. What is an object?
An object can be consider as a real-world entity which is the basic part of OOPs for like cat, chair, mouse, dog, etc. Multiple objects have multiple states or attributes, and behaviors.
5. What is the difference between a class and a structure?
Class: User-defined blueprint from which objects are created with different behavior. It have methods or set of instructions which is to be performed on the objects.
Structure: A structure is also a user-defined collection of different data type variables.
6. What is the approach for call the base class method without creating an instance?
There is two approach to call base class method with creating instance
- If it is a static method
- The base class must be inherited by some other subclass
7. What is an object?
- An object can be a real-world entity which is an instance of a class and act like a variable of class.
- An object can be a physical entity and occupied space when created
8. What is a class?
- A class can be a template or a blueprint of object within which we can create object.
- A class binds methods and data together into a single unit.
- A class is a logical entity does not occupied memory space when created.
9. What is inheritance?
Inheritance is a one of the feature of OOPs which allows classes to inherit common properties from other classes or parent class. This feature help us to get rid of redundant code hence reducing the overall size of the code.
10. What are the types of inheritance?
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
11. What is the multiple inheritance?
Multiple inheritance means when a class inherits more than one base class is know as multiple inheritance.
12. What is the multilevel inheritance?
Multilevel inheritance can be defined as when a class inherits from another class which itself is a subclass of some other base class
13. What is hybrid inheritance?
Hybrid inheritance can be achieve by combining of of two or more inheritance.
14. What is hierarchical inheritance?
Hierarchical inheritance can be defined as when an inheritance in which one base class has more than one subclasses.
15. What is a superclass?
A superclass or base class can be is a class which acts as a parent to some other class or classes
16. What is a subclass?
A class which inherits from another class is called the subclass and the class which is inherited is called superclass or parent class.
17. What is polymorphism?
When an object shows different different behavior at different different circumstances then the behavior of an object is known as polymorphism
18. What is static polymorphism?
The polymorphism which happens at compile time is know as Static polymorphism (static binding) like method overloading.
19. What is dynamic polymorphism?
The polymorphism which happens at runtime is known as dynamic polymorphism like method overriding.
20. What is method overloading?
Define two or more method in a same class that share the same name but their no of parameter and order of parameter and return type are different is called method overloading.
21. What is method overriding?
When a method in sub class has the same name, return type and parameter as the method in the super class then the method in the subclass is override the method in super class.
22. What is encapsulation?
Encapsulation is a part of oops concept which wrap the data and function into a single unit.
23. What are access modifiers?
- Public:-can b accessed by class or method
- Protected:- accessed by the class of same package or sub class of this class or within the same class.
- Default:- Accessible within the package only.
- Private:- Accessed within the class only.
24. How to achieve data abstraction?
Data abstraction can be achieved through:
- Abstract class
- Abstract method
25. What is an abstract class?
An abstract class can be a class that consists of abstract methods only. These abstract methods are basically declared but not defined. If these methods are need to be used in some subclass, they need to be defined in the subclass.
26. How can we create an instance of an abstract class?
No it is not possible. As abstract class does not have a complete implementation so we can not create instances of an abstract class . But we can create instances of subclass inheriting the abstract class .
27. What is an interface?
It is a concept of OOPs in which we declare methods without defining them. Interfaces do not have detailed instructions or actions which is to be performed. Any class which implements an interface defines the methods of this interface.
28. What are virtual functions?
Virtual functions are those functions that are present in the parent class and are overridden by the subclass. These functions are used to achieve runtime polymorphism.
29. What is a constructor?
A constructor can be consider as a special type of method that has the same name as the class and is used to initialize objects of that class.
30. Types of constructors.
Types of constructors differ from language to language. However, all the possible constructors are:
- Default constructor
- Parameterized constructor
- Copy constructor
- Static constructor
- Private constructor
31. What is a copy constructor?
A copy constructor is used to creates objects by copying variables from another object of the same class. The main purpose of a copy constructor is to create a new object from an existing one.
32. What is a final variable?
A variable whose value does not change.
33. What are the limitations of OOPs?
- Usually not suitable for small problems.
- Requires intensive testing.
- Takes more time to solve the problem.
- Requires proper planning.