|
81.When do you overload a method in Java and when do you override it?
- Rather a simple question for experienced designer in Java.
- if you see different implementation of a class has different way of doing certain thing than overriding is the way to go while overloading is doing same thing but with different input.
- method signature varies in case of overloading but not in case of overriding in java.
82.Design ATM Machine?
- We all use ATM (Automated Teller Machine) , Just think how will you design an ATM ? for designing financial system one must requirement is that they should work as expected in all situation.
- so no matter whether its power outage ATM should maintain correct state (transactions), think about locking, transaction, error condition, boundary condition etc.
- even if you not able to come up exact design but if you be able to point out non functional requirement, raise some question , think about boundary condition will be good progress.
83.You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombatand may be even to direct exchange feed , how do you design your Market Data system.?
- This is very interesting design interview question and actually asked in one of big investment bank and rather common scenario if you have been writing code in Java.
- Key point is you will have a MarketData interface which will have methods required by client e.g. getBid(), getPrice(), getLevel() etc and MarketData should be composed with a MarketDataProvider by using dependency injection.
- So when you change your MarketData provider Client won't get affected because they access method form MarketData interface or class.
84.Why is access to non-static variables not allowed from static methods in Java?
- You can not access non-static data from static context in Java simply because non-static variables are associated with a particular instance of object while Static is not associated with any instance.
- You can also see my post why non static variable are not accessible in static context for more detailed discussion.
85.Design a Concurrent Rule pipeline in Java?
- Concurrent programming or concurrent design is very hot now days to leverage power of ever increasing cores in advanced processor and Java being a multi-threaded language has benefit over others.
- Do design a concurrent system key point to note is thread-safety, immutability, local variables and avoid using static or instance variables.
- you just to think that one class can be executed by multiple thread a same time, So best approach is that every thread work on its own data, doesn't interfere on other data and have minimal synchronization preferred at start of pipeline.
- This question can lead from initial discussion to full coding of classes and interface but if you remember key points and issues around concurrency e.g. race condition, deadlock, memory interference, atomicity, ThreadLocal variables etc you can get around it.
86.What is design patterns ? Have you used any design pattern in your code ?
- Design patterns are tried and tested way to solve particular design issues by various programmers in the world.
- Design patterns are extension of code reuse.
87.Can you name few design patterns used in standard JDK library?
- Decorator design pattern which is used in various Java IO classes, Singleton pattern which is used in Runtime , Calendar and various other classes, Factory pattern which is used along with various Immutable classes likes Boolean e.g. Boolean.
- valueOf and Observer pattern which is used in Swing and many event listener frameworks.
88.What is Singleton design pattern in Java ? write code for thread-safe singleton in Java?
- Singleton pattern focus on sharing of expensive object in whole system.
- Only one instance of a particular class is maintained in whole application which is shared by all modules.
- Java.lang.Runtime is a classical example of Singleton design pattern.
- You can also see my post 10 questions on Singleton pattern in Java for more questions and discussion.
- From Java 5 onwards you can use enum to thread-safe singleton.
89.What is main benefit of using factory pattern ? Where do you use it?
- Factory pattern’s main benefit is increased level of encapsulation while creating objects.
- If you use Factory to create object you can later replace original implementation of Products or classes with more advanced and high performance implementation without any change on client layer.
- See my post on Factory pattern for more detailed explanation and benefits.
90.What is observer design pattern in Java?
- Observer design pattern is based on communicating changes in state of object to observers so that they can take there action.
- Simple example is a weather system where change in weather must be reflected in Views to show to public.
- Here weather object is Subject while different views are Observers.
- Look on this article for complete example of Observer pattern in Java.
91.Give example of decorator design pattern in Java ? Does it operate on object level or class level?
- Decorator pattern enhances capability of individual object.
- Java IO uses decorator pattern extensively and classical example is Buffered classes like BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance.
- Read more on Decorator design pattern and Java
92.Define creational design patterns?
- Creational patterns are used to define and describe how objects are created at class instantiation time.
- Usually an abstract super class contains the details of the classes that are instantiated and the client class is unaware of such details.
- Singletion pattern, factory method pattern, abstract factory pattern are examples of creational design pattern.
93.A class instance can be created using new operator. Why should we use creational design patterns to create objects?
- Using new operator to create objects is a valid approach but it�s like hard coding the object type.
- If we are 100% sure that our object will be of the same class all the time, then we use new operator to create an object.
- In scenarios where the nature of the object can change according to the nature of the program, we use creational design patterns which offer flexible approach for creating class instances.
94.Which object oriented method is used by the creational patterns to instantiate object?
- Creational patterns use inheritance to decide the object to be instantiated.
95.Define a factory with respect to software development?
- Object construction is referred as a factory.
- It is used to isolate the creation of objects from their usage.
- If you are using factory methods for objects creation, then the new derived types will cause no code change in your classes that are using factories.
96.When will you use a Factory Pattern?
- The factory pattern is preferred in the following cases: - a class does not know which class of objects it must create - factory pattern can be used where we need to create an object of any one of sub-classes depending on the data provided
97.Give an example of factory method that creates and return an object based on a String parameter. You have following interface. Create 2 implementations for it and write a factory method for it?
packagefactory.pattern;
publicinterfaceIMessage {
void sendMessage(String phNr, String messageContent);
}
SMSMessage and MMSMessage are implementing IMessage and MessageFactory class provides a factory method getMessageSender(String arg) method.
packagefactory.pattern;
publicclassSMSMessage implements IMessage{
@Override
public void sendMessage(String phNr, String messageContent) { System.out.println(String.format("Sending SMS contnet: %s to %s", messageContent, phNr));
}
}
packagefactory.pattern;
publicclassMMSMessage implements IMessage{
@Override
public void sendMessage(String phNr, String messageContent) {
System.out.println(String.format("Sending MMS contnet: %s to %s", messageContent, phNr)); }
}
packagefactory.pattern;
publicclassMessageFactory {
public static IMessage getMessageSender (String arg){
IMessage message = null;
if(arg != null){
if(arg.equals("SMS")){
message = new SMSMessage();
} else if (arg.equals("MMS")){
message = new MMSMessage();
}
}
return message;
}
}
98.Given below is a class that provides a static factory method. Write a class to use this factory method?
Code
packagefactory.pattern;
publicclassMessageFactory {
public static IMessage getMessageSender (String arg){
IMessage message = null;
if(arg != null){
if(arg.equals("SMS")){
message = new SMSMessage();
} else if (arg.equals("MMS")){
message = new MMSMessage();
}
}
return message;
}
}
// IMessage interface - it can have many implementations
packagefactory.pattern;
publicinterfaceIMessage {
void sendMessage(String phNr, String messageContent);
}
Based on the command line argument, factroy method will return an implementation of IMessage.
packagefactory.pattern;
publicclassMainTestClass {
public static void main(String[] args) {
IMessage messageSender = MessageFactory.getMessageSender(args[0]);
if(messageSender !=null){
messageSender.sendMessage("1234", "Hello World");
}
}
}
99.Name the creational design pattern that provides abstraction one level higher than the factory pattern?
- Abstract factory pattern provides abstraction one level higher than factory pattern.
- It encapsulates a group of individual factories.
100.Write example of abstract factory pattern using following interfaces.?
interfaceGUIFactory {
public Button createButton();
}
interfaceButton{
public void paint();
}
- WInFactory and OSXFactory are the implementations of GUIFactory.
- TestApplicationRunner provides createOsSpecificFactory method that returns GUIFactory.
classWinFactory implements GUIFactory {
public WinButton createButton() {
return new WinButton();
}
}
classOSXFactory implements GUIFactory {
public OSXButton createButton() {
return new OSXButton();
}
}
classWinButton implements Button {
public void paint() {
System.out.println("I'm a WinButton");
}
}
classOSXButton implements Button {
public void paint() {
System.out.println("I'm an OSXButton");
}
}
classApplication {
public Application(GUIFactory factory) {
Button button = factory.createButton();
button.paint();
}
}
publicclassTestApplicationRunner {
public static void main(String[] args) {
new Application(createOsSpecificFactory(args[0]));
}
public static GUIFactory createOsSpecificFactory(String arg) {
if (arg.equals("0)) {
return new WinFactory();
} else {
return new OSXFactory();
}
}
}
|