When instantiating a generic class, only reference type arguments are passed to the type parameters.
Hence, the correct answer is option “B”.
Explanation of Solution
Reference types only:
- Only non-primitive types can be substituted for type parameters when creating instances for generic class.
- Reference types are non-primitive types.
- The compiler autoboxes the primitive type values to reference types.
Explanation for incorrect options:
a. Primitive types only:
When creating instances for a generic class, only non-primitive types can be substituted for type parameters.
Hence, the option “A” is wrong.
c. Interface types only:
The type parameters can be specified as interface type but the arguments passed to type parameters at the time of instantiation, must be real types such as Integer, String, and so on.
Hence, the option “C” is wrong.
d. Primitive, reference, and interface types:
When creating instances for a generic class, only reference types can be substituted for type parameters.
Hence, the option “D” is wrong.
Want to see more full solutions like this?
Chapter 17 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Describe how you can use an interface as an alternative to a class to specify the type for a parameter.arrow_forward3. The interface Range is defined below:public interface Range{ void setLimits(int x, int y); int getNumber();} Implementations of Range should exhibit the following behaviour:• The setLimits method should store its parameter values somewhere to serve as the lower and upper limits of the range appropriately.• A call to getValue should return an integer that is always greater than or equal to the lower limit and less than or equal to the upper limit.The class RandomInteger is an implementation of the interface Range. It also has the following features:• It has two fields of int type, low and high, to record the lower and upper limits of the range, respectively.• It has single constructor method that takes two integers and stores them in the appropriate fields. Note: You should make use of the setLimits method.• A call to the method getNumber should return a random number that is always within the range, i.e. greater than or equal to the lower limit and less than or equal to the upper…arrow_forwardQ1.(java)Create a generic class with a type parameter that simulates drawing an item at random out of a box. This class couldbe used for simulating a random drawing. For example, the box might contain Strings representing names writtenon a slip of paper, or the box might contain Integers representing a random drawing for a lottery based on numericlottery picks. Create an add method that allows the user of the class to add an object of the specified type along withan isEmpty method that determines whether or not the box is empty. Finally, your class should have a drawItemmethod that randomly selects an object from the box and returns it. If the user attempts to drawn an item out of anempty box, return null. Write a main method that tests your class.arrow_forward
- The Java Programming class has two types – remote and in-person. For remote students, the weighted score comprises of midterm, final, assignments and discussion. The weights for each are 30%, 30%, 30% and 10% respectively. For in-person students, the weighted score comprises of midterm, final and assignments. The weights for each are 30%, 30% and 40%. Write a generic class Students.java which has a constructor that takes three parameters – id, name, and type. Type will represent if the student is ‘remote’ or ‘in-person’. A toString() method in this class will display these details for any student. A generic method score() will be part of this class and it will be implemented by inherited classes. Write accessors and mutators for all data points. Write two classes RemoteStudents.java and InPersonStudents.java that inherits from Student class. Show the use of constructor from parent class (mind you, RemoteStudents have one additional parameter – discussion). Implement the abstract method…arrow_forwardWrite a java program that Create a generic class AnimalList that has a type parameter T. It should have at least two properties declared as the passed type, constructors), getters, setters, and toString methods. Create a superclass Animal that has two properties (String, int, etc.), and constructor, getters, setters, and toString methods. Create two subclasses of Animal called Cat and Dog. Each class should have a unique property and method. Create a class Lizard with a method of your choice. Create a main class AnimalTest that creates two instances of Animal: Dog type and Cat type. Create a method that takes Animal type as parameter constraining the types to be subclasses of the Animal class only; check the type of object and call the unique method in the Dog and Cat. Create an instance of the Lizard class and test it. Display all objects.arrow_forward1. Consider the header of this class: public class AList implements iList Which type of class is AList? (A. Interface class, B. Derived class, C. Base class, D. Generic Class)arrow_forward
- Suppose that you are in charge of customer service for a certain business. As phone calls come in, the name of the calleris recorded and eventually a service representative return the call and handles the request. Write a class ServiceRequests that keeps track of the names of callers. The class should have following methods: addName(name) - adds a name to the list of names. Throws a ServiceBackUpException if there is no free space in the list.removeName(name) - removes a name from the list. Throws a NoServiceRequestException if the name is not on the list.getName(i) - returns the ith name in the list.getNumber -returns the current number of service requests. Write a program that uses an object type ServiceRequests to keep track of customers that have called.It should have a loop that, in each iteration, attempts to add a name,remove a name, or print all names. Usean array of size 10 as the list of names.arrow_forwardI need to know how to do this in python: The base class Pet has attributes name and age. The derived class Dog inherits attributes from the base class Pet class and includes a breed attribute. Complete the program to: Create a generic pet, and print the pet's information using print_info(). Create a Dog pet, use print_info() to print the dog's information, and add a statement to print the dog's breed attribute. Ex: If the input is: Dobby 2 Kreacher 3 German Schnauzer the output is: Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: German Schnauzerarrow_forwardWhich is illegal.Single choice. public class classA extends classB public class classA extends classB,classC public class classA implements interfaceA public class classA implements interfaceA,inetrfaceBarrow_forward
- The interface Range is defined below:public interface Range{ void setLimits(int x, int y); int getNumber();}Implementations of Range should exhibit the following behaviour:• The setLimits method should store its parameter values somewhere to serve as the lower and upper limits of the range appropriately.• A call to getValue should return an integer that is always greater than or equal to the lower limit and less than or equal to the upper limit.The class RandomInteger is an implementation of the interface Range. It also has the following features:• It has two fields of int type, low and high, to record the lower and upper limits of the range, respectively.• It has single constructor method that takes two integers and stores them in the appropriate fields. Note: You should make use of the setLimits method.• A call to the method getNumber should return a random number that is always within the range, i.e. greater than or equal to the lower limit and less than or equal to the upper limit.…arrow_forwardC# (Generic Method IsEqualTo) Write a simple generic version of method IsEqualTo that compares its two arguments with the Equals method, and returns true if they’re equal and false otherwise. Use this generic method in a program that calls IsEqualTo with a variety of simple types, such as object or int. What result do you get when you attempt to run this program?arrow_forwardwhat is the generic term for a method that creates an instance of a class (constructors?) and how would you call it for a class named MyClass that does not take any parameters?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education