Interview questions on concept of code on interface rather then class

Oops concept is always a must to have skill by an interviewee. Today i am going to explain interview question-answer and practical implementation of one oops concept which says Code on Interface rather then a Class.

Question1 : What does code on interface rather then class means.
Answer : This is an oops concept which says that we should call methods of a class with the reference of it’s interface rather then that class itself.

Question2 : Then interviewer asked what is the benefit of using interface reference .
Answer : I explained it with the help of example. If we have a restro-bar entity Which have both food items and drink items as its methods . When we go in restro bar and we want to drink then they will provide us two seperate menu for drink and eat(two interfaces), so that we can choose easily between drinks items(drink interface) and inspite of restrobar(class) have eat items too but it won’t bother me . So having two seperate interfaces made customer choose easily between items according to their concerns .


public interface DrinkMenu {
public List drinkItems();
}
public interface FoodMenu {
public List foodItems();
}

public class RestroBar{
public List drinkItems(){
List drinkItems=new ArrayList();
//add drink items
return drinkItems;
}
public List foodItems(){
List foodItems=new ArrayList();
//add food items
return foodItems;
}
}

Question 3 : Then interviewer asked me what if i want to order food from different restaurants daily but does not want to give different websites to order, how could i achieve this.
Answer : Now definitely he wants to understand if i can use interface practically and solve his problem. So i told him to solve this problem we need an interface having method which will return list of food items and different restaurants(Classes) will implement this method and will return filled list with their food items.

public interface Restaurants {
public List foodItems();
}
public class ChawlaRestaurant{
public List foodItems(){
List foodItems=new ArrayList();
//Chawla food items
return foodItems;
}
}
public class HaweliRestaurant{
public List foodItems(){
List foodItems=new ArrayList();
//Haweli food items
return foodItems;
}
}

Now we need a factory which will return Restaurants interface reference holding particular restaurant object by taking restaurant name as input.


public class ObjectFactory{
public Restaurants getRestaurantObject(String restaurantName){
Restaurants restaurants = //By using reflection we can create obect
return restaurants;
}
}

Now in main class which will be access by user we will get hotel name input from user then that restaurants object from factory at run time and then food items of that particular restaurant.


public class UserInput{
public class displayFoodItems(String restaurantName){
ObjectFactory objectFactory=new ObjectFactory();
Restaurants restaurants = objectFactory.getRestaurantObject(restaurantName);
List foodItems = restaurants.foodItems();
for(String foodItem : foodItems){
system.out.println(foodItem);
}
}
}

Please Ask Questions or give suggestions through comments in below section .

Other related posts are :
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Top 10 spring interview questions and their answers. Spring basic concepts.
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
MQ connection not closed and giving MQ Error-2009

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

Interview question on same name entity twice in Jpa/Hibernate

Recently when one of my friend went for an interview then interviewer trap him into an interesting question and he discussed with me, so i thought to share his experience .

Question 1 : Interviewer asked him if we can have two entity for a single table .
Answer : He said yes we can have two entity for same table name if we want to, JPA/Hibernate does not stop us from doing that .

Question 2 : Then interviewer asked him, What is DuplicateMappingException : duplicate import.
Answer : Now my friend got stuck because he does not know answer of this question . So he came too me after interview and discuss it with me . Then i told my friend that this exception occurs when we have two same name java class having @entity annotation in different package. Actually what JPA does that it gave a unique name to each entity you define with @entity annotation as defined below .


package com.google.employee;
@Entity
@Table(name="GOOGLE_EMPLOYEE")
public class Employee{
}

package com.ask.employee;
@Entity
@Table(name="ASK_EMPLOYEE")
public class Employee{
}

As we did not provide any name inside @Entity annotation so, JPA gave class name as entity name. JPA does not allow two same name entity and will give exception Entity name must be unique in a persistence unit . So if we want to have two same name classes in different package as our entity then we should provide a name as given below.


package com.google.employee;
@Entity("GoogleEmployee")
@Table(name="GOOGLE_EMPLOYEE")
public class Employee{
}

package com.ask.employee;
@Entity("AskEmployee")
@Table(name="ASK_EMPLOYEE")
public class Employee{
}

and we will use this name in named queries after from clause too.

select askEmployee from AskEmployee askEmployee where askEmployee.employeeName=:emplyeeName

Please Ask Questions or give suggestions through comments in below section .

Other related posts are :
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Top 10 spring interview questions and their answers. Spring basic concepts.
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
MQ connection not closed and giving MQ Error-2009

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

Are Payment banks a competitive threat or addon to the traditional banking.

Few Days before i read a news that RBI governor “Mr. Raghuram Rajan” announced 13 new banks, Infact 2 banks and 11 payment banks.
Raghuram Rajan
First thing came in my mind was that how these so called traditional banks let that happen , I mean who like the competition at such a vast level . Then i thought what if these payment banks are not competition to them, infact this is what existing banks wants to happen from a long time. Now you ask why ???

Actually retail customer and their small transactions are overhead to these traditional banks they only like Corporate user,Loan user(retail and corporate both),Credit card users etc. but they have to entertain retail user because of RBI norms. Now days when retail customer is purchasing approximately everything online and making many transaction in one day, this overhead is increasing more and more to these banks . On the other hand these payment banks will be very happy to entertain these customers because their business model will depend on selling things online, collaboration with merchant sites(Amazon, Flipkart, Snapdeal etc), online advertisements etc, So they will not have any conflict or competition with each other.
In payment banks you can have money upto 1 lakh,they will provide interest on that money like saving account. So now question arise if a retail customer like you and me have both options, why i will go for payment bank instead of existing bank. So to attract customers these banks will start providing cashbacks on online purchases , which existing banks don’t do .

Payments banks add-ons, not competitors: Raghuram Rajan

Other related posts are :
How to render HTML in java
Top 10 spring interview questions and their answers. Spring basic concepts.
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
Set datasource spring bean properties dynamically if server is not available

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.