For the following piece of Java code:
a) State which object creation mechanism is being used
b) Explain how you determined which object creation mechanism is being used
c) Complete the missing portions of the code (indicated by underscores)
____________________ part1
{
public void function1(String someInput);
public double function2();
public int function3(int thisVal, int thatVal);
}
public ____________________part2 ____________________
{
double aNumber;
public void function1(String someInput)
{
System.out.println(someInput);
}
public double function2()
{
aNumber = 6.0;
return aNumber;
}
public int function3(int thisVal, int thatVal)
{
return thisVal * thatVal;
}
}
a)
Answer: Factory method creational design pattern object creation mechanism is used
b)
Answer:
Since the above has an interface part1 and a class part2 that implements part1, the interface part1 has only abstract methods and all the implementing classes must provide implementation for these methods.
The above code gives polymorphic behaviour.Since here we have defined an interface for creating an object, but subclasses decide which class to instantiate thus it is a factory method object creaion mechanism
c)
Ans) public interface part1
public class part2 implements part1
Get Answers For Free
Most questions answered within 1 hours.