Monday, July 9, 2018

Java Interview Questions

1. What is OOP
A collection of objects. Each object is an instance of a class.

2. Concepts of OOP
Abstraction                                        Java.lang.Object is the super base class 
Encapsulation                                    Every class descends from Object.
Inheritance                                         
Polymorphism  

3. What is a class
A representation of a type of Object. It is a blueprint that describes the details of an object.

4. What is Polymorphism  
One name, many forms. Multiple methods all with the same name but slightly different functionality. 
Overriding -->run time polymorphism
Overloading --> compile time polymorphism

Method overloading
Method overriding
Parameters must be different
Parameters should be the same
It is compile time polymorphism
It is run time polymorphism
Return type can be same or different
Return type must be the same


5. What is encapsulation
It contains all data which is hidden. That hidden data can be restricted to the members of that class.
Access specifiers --> Public
                               Protected
                               Private
                               Default
Public Access Modifier - Public
A class, method, constructor, interface, etc. declared public can be accessed from any other class.
if the public class we are trying to access is in a different package, then the public class still needs to be imported.

Protected Access Modifier - Protected

Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, methods and fields in an interface cannot be declared protected.
Protected access gives the subclass a chance to preventing a nonrelated class from trying to use it.
Private Access Modifier - Private
Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.
Using the private modifier an object will encapsulates itself and hides data from the outside world.

Default Access Modifier - No Keyword

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
6. What is inheritance
A concept where one class shares the structure and behaviour defined in another class. If inheritance applied to one class - single inheritance
If it depends on multiple classes - multiple inheritance

7. What is multiple inheritance
Java does not support multiple inheritance where a class can inherit properties of more than one parent class.
C++ supports multiple inheritance
In java multiple inheritance is not supported but it is supported in interfaces. A single interface can be extend multiple interfaces because interfaces only declare the methods but the actual implementation will happen in concrete classes by implementing the interfaces.

8. What is abstraction
Hiding the implementation details from the user only the functionality will be provided to the user.
Achieved using abstract classes  and interfaces.

9. Whenever you need to reuse the code, when there's a IS-A relationship between classes whenever you can move the code to the super class what would you use?
Inheritance

10. Whenever needs to represent some common actions between two classes what to use?
Interfaces (using implements keyword)

                  

No comments:

Post a Comment