Name the following:
(i) An OOP principle which implements data abstraction
(ii) A function which converts a String type value to double
Quick please answer...I have my final exam tomorrow!!
Answers
Answer:
i) Abstraction using classes: An abstraction can be achieved using classes. A class is used to group all the data members and member functions into a single unit by using the access specifiers. A class has the responsibility to determine which data member is to be visible outside and which is not.
ii) There are three ways to convert a String to double value in Java, Double.parseDouble() method, Double.valueOf() method and by using new Double() constructor and then storing resulting object into a primitive double field, autoboxing in Java will convert a Double object to the double primitive in no time.
Explanation:
i) Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract class/Interface we express the intent of the class rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough.
ii)Out of all these methods, the core method is parseDouble() which is specially designed to parse a String containing floating point value into the Double object. Rest of the methods e.g. valueOf() and constructor uses parseDouble() internally. This method will throw NullPointerException if the string you are passing is null and NumberFormatException if String is not containing a valid double value e.g. containing alphabetic characters.