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)

                  

Monday, November 16, 2015

What is Abstraction ?


Abstraction is hiding the implementation from the user. It will only focus on the essentials and it will ignore the irrelevant. It will shows only the functionality. Java Abstraction is achieved using Abstract classes, and Interfaces.

If we take an example of a chair we will just mention it as a chair we won’t say it is made of wood or glass and how it was made like wise. We will hide the irrelevant parts.

Abstraction Example:


If we take an example of an employee when we write all the properties of the employee it will look like this.

public  class Employee
{
   private String name;
   private String address;
   private String gender;
  
   private int number;
   private int salary;
   private int allowance;
  
   void getDetails(){
          //details methods
   }
  
    double computePay() {
       //compute the salary
              return 0;
       }
  
 
}

But we can see that rather than having an employee class we can group details and the salary to another group. So it will look like this.


public class Salary {
  private int number;
   private int salary;
   private int allowance;
  
double computePay() {
       //compute the salary
              return 0;
       }
     
}


public class Details {
  private String name;
  private String address;
  private String gender;

     void getDetails(){
          //details methods
   }  
}

So you can see the attributes and the methods realated to the Details and Salary are moved.

From having java abstraction we can  reduce the complexity and also it will improve the maintainability of the system.

Sunday, November 15, 2015

Final keyword in java

Final keyword is used to restrict the user. You cannot change it after you made it final. Java final keyword is used to stop changing the value, stop method overriding & to stop inheritance. Final can be
      variables
      methods
       classes

final variables

If you make any variable as final you cannot change the final value of it it will be a constant.

Ex:
public class Demo {
      
       public int MAX_VAL= 11;
      
       public void myMethod(){
               MAX_VAL= 12;
             
       }

       public static void main(String[] args) {
              Demo demo= new Demo();
              demo.myMethod();

       }

}
This will give u an compile error as we have changed the value of the MAX_VAL.





Final methods
Once the method is final you cannot override it.

Ex:


final classes
If u make the class final you cannot extend it.

Ex:
Coding example


public class TestRun {

       public static void main(String[] args) {
              Test test = new Test();
              System.out.println(test.p1);
       }

}


class Test{
       public double  p1=3.14;
       Test(){
              p1=2.14;
       }
}


How to avoid someone changing the p1 ?
We can have p1 as final. But when you make it as final you will get an error like this.