In: Computer Science
Consider the following statement: When designing a data structure, it is important to distinguish between what the characteristics and operations of the data structure are versus how they can be implemented.
(a) Explain why defining the ADT as a Java interface and defining classes to implement the interface is consistent with the above statement.
(b) Describe how the setup in part (a) is an example of polymorphism and provide a specific example of how polymorphism can be used to make our designed data structures more useful.
(c) Provide two (2) examples of how the specifications of the Java interface can enforce rules about how a data structure behaves. Be specific and explain your answer.
(d) Provide three (3) examples of how implementation-level decisions allowed a particular implementation to improve the runtime efficiency of an operation. Be specific and explain your answer.
Ans.
a. Abstraction is a process of hiding the details from the user and providing only the functionality i.e. hiding the implementation and showing only essential functionality. Abstraction is done for security. It reduces the complexity for users. Some of the common ADT's are List ADT, stack ADT, queue ADT etc. Interface is used in JAVA to establish abstraction. Interfaces can be considered as pure ADTs because an interface can be implemented many times and user will only see the functionality and not the implementation. Multiple Inheritance can also be achieved through interfaces in JAVA as there will be no ambiguity, as function of interface can be overridden ‘n’ number of times. Thus interfaces are consistent with the statement as we can distinguish between its working and implementation and can achieve abstraction easily by using interfaces in Java.
b. Polymorphism is another pillar of OOPs concept where the same code or functionality is used again in some different form. It is used to achieve code re-usability. There are two types of polymorphisms: runtime polymorphism and compile time polymorphism. The example of runtime polymorphism is method over-riding where name of the methods and parameters are same but have different functionalities in child classes and example of compile time polymorphism is method over- loading where the name is same but parameters differ in some or the other manner. We always over-ride functions declared in interfaces in the class which implements the interfaces and by this way polymorphism is achieved as we always need to over-ride functions to implement interfaces through classes and this is the same case with other ADTs too. Thus polymorphism is achieved.
c. Collection framework is basically a framework which provides a group of objects such as lists, maps, queues, hashmaps etc. In the collection framework there are interfaces and classes. The collection framwork is a complete group of data structures and lists, queue, sets, etc. are interfaces and thus here interfaces are enforcing rules by providing methods to implement, thus deciding how data structures behave.
d. Interfaces only provide declaration, so the main part depends upon the class which is implementing the interface and over-riding its functions as it's in the hands of user to use right data structures and logic which will increase efficiency of the code. We could always have a better logic to a problem that we can use and with that we can achieve better efficiency to the code. If one's implementation is complex then there will always be chances of logic to under-perform.