Interface In Java 8

Interface :

It is similar to a class in java, instead, with a keyword as interface. 
Interface used as method declaration, not for implementation untill jdk7

Eg:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
Public interface MyInteface {
 Void m1(String str);
 Void m2(int i);

 // But you cannot write as follows in interface
 /*Void m3(String s){
 System.out.println("You cannot write this way. "); 
     }*/

}




Interface in Java-8 :

Java-8 interface can have STATIC DEFAULT methods, we can also have method implementation from jdk 8 but has to be default

Eg: Before JAVA-8 

1
2
3
4
5
6
7
Public interface MyInterface{
     Void m1(String str);
     /* Default void m2(String str){
          System.out.println(“Gives an error in prior jav-8 versions. ”);
    } //Gives you an error
  
  */
}



Code In Java-8

Java-8 Functional Interface

Java Functional Interface



If look into some other programming languages like c++, Javascript they are called functional programming languages bcoz, we can write functional and use whenever required.

An interface with only 1 abstract method is called Functional interface.
A new annotation @FunctionalInteface provides us to avoid accidental addition od abstract methods.
Functional interface is long waited and much sought out feature of Java-8 as it enables us to use Lambda expressions to instantiate them.

Being object-oriented is not bad, but when we wanted to implement Runnable we do it as follows

Eg: 


1
2
3
4
5
Runnable r = new Runnable(){
   @Override
   public void run() {
    System.out.println("My Runnable");
   }};

If you notice it, the important logic in under run() method right?

Java-8 lambda expressions allow to help us in writing smaller and cleaner code.


Lambda Expressions:

Lambda expression is way through we can visualize functional programming in java object world.
Objects are base of java prog. Lang. and we can never have a function without an Object


Syntax:  (argument) -> (body)


1
Runnable r1 = () -> System.out.println("My Runnable");


In the above code, Runnable is a functional interface so we can use Lambda.
Since run() has no arguments,  we have no arguments passed.

Advantages of Lambda expressions:
  1. Reduced lines of code.
  2. Pass method into behaviours
  3. High efficiency 

Tell me about yourself

Tell me about yourself


Most of the interviewers ask us to 'tell me about yourself ' and the way you tell about your past projects, roles, and your background could be an icebreaker. If they like it there are 70% chances of getting hired.


Best ways to answer are:

      1. Attempt confidently

Yes, you heard it right!! Tell very confidently, because lack of confidence rises a doubt about your background. If the candidate isn’t confident how can your team and manager trust you?? Not possible right. So go with confidence.


Spring Questions

  • What is Spring Beans??

Java Objects which form the backbone of Spring application and they are assembled, instantiated, and managed by Spring IoC container.

These beans are created with configuration metadata that is supplied to the container, for instance, in the XML <bean/> form definitions.


  • How to provide configuration metadata to the Spring Container?

3 important methods to provide configuration metadata to the Spring Container :

    • Annotation-based configuration
    • Java-based configuration
    • An XML based configuration file

  • Explain the different types of bean scopes supported by Spring?

There are 5 different scopes provided by the Spring Framework:
    • Prototype scope, a single bean definition has any number of object instances.
    • Singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container.
    • Request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware -Spring ApplicationContext.
    • Session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a -web-aware Spring ApplicationContext.
    • Global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext.

  • Is Singleton class thread safe in Spring?
    NO


  • Explain Bean lifecycle in Spring framework

The spring container finds the beans definition from XML file, and instantiates the bean and also Spring populates all the properties specified in the bean definition (DI).

    • If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.
    • If Bean implements BeanFactoryAware interface, spring passes the bean-factory to setBeanFactory() method.
    • If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesser beforeInitialization() method.
    • If the bean implements IntializingBean, its afterPropertySet() method is called.
    •  If the bean has init method declaration, the specified initialization method is called.
    • If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
    • If the bean implements DisposableBean, it will call the destroy() method.


What happens when you click on SUBMIT button [Spring MVC]

What happens when you click on SUBMIT button 

[Spring MVC] ??



1. When you click on submit the request goes to web.xml
2. Spring has a special servlet called as dispatcher servlet[front controller] 
3. From dispatcher servlet, it will go to the corresponding Controller
4. From controller, it redirects to business object and performs validations 
                              if it passes all the validation it goes to Data access layer 
                              else returns back to the user
5. From DAO it goes to DB 


 Dispatcher servlet  >> vo >> controller >> BO             >>                  DAO  >>  DB                                                                          Validations                                                                         if pass goes to DAO