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.

Advertisements

One thought on “Interview questions on concept of code on interface rather then class

  1. Pingback: Concept of AOP (Aspect oriented programming) every spring programmer use without knowing about it . | Techno Tailor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s