Question

While String is a sub-type of Object, it is not true that Bag<String> is a sub-type...

While String is a sub-type of Object, it is not true that Bag<String> is a sub-type of Bag<Object>. Explain why?

(Hint: If A "is a" B, then everything you can do with B you can also do with A. What can you do with a Bag<Object> than you cannot do with Bag<String>?)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Bag<String> cannot be a subtype of Bag<Object> unless and until it is specifically inherited.

However, the advantage in having Bag<Object> is that it allows to use classes that are derived from Object.

i.e we can add an Integer and String into the Bag<Object> for the same instance.

For example:

class Bag<E>
{
   E data;
   Bag(E data)
   {
       //do something
   }
}
class sample
{
   public static void main (String[] args) throws java.lang.Exception
   {
       //both string and integer is possible
       Bag<Object> bag = new Bag<Object>("Hello");
       bag = new Bag<Object>(23);
   }
}

Add a comment
Know the answer?
Add Answer to:
While String is a sub-type of Object, it is not true that Bag<String> is a sub-type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

  • Python Language Problems 1. isdigit() A. Returns True if this string is just a digit. B....

    Python Language Problems 1. isdigit() A. Returns True if this string is just a digit. B. Returns True if this string contains only number characters. C. Return True if this string has at least one number character. D. is not a valid function. 2. Pick the most accurate interpretation for the if construct: if letter in words: print("Got it") A. If the character letter existed in the string words, it prints "Got it". B. It traverse through the string words,...

  • 1.     When a sub class object is created, when is the call to the super class...

    1.     When a sub class object is created, when is the call to the super class constructor made? How does a programmer call the super class constructor from the sub class? What do all classes indirectly extend? What methods does every class inherit from the Object class? 2.     When writing methods in a sub class, how can those methods call the methods from the parent class? 3.     Which class is more specific, a super class or a sub class? 4.    ...

  • In this lab, we will implement the Linked Bag. The bag will contain a sequence of...

    In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to thenext Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Data Next Node Then we will design another class named LinkedBag (LinkedBag.java). LinkedBag class has an instance variable called firstNode of...

  • IN JAVA 38.   What is the return type of the String class compareTo( ) method? A) boolean...

    IN JAVA 38.   What is the return type of the String class compareTo( ) method? A) boolean B) int C) String D)coid 39.    What is the return type of the constructor method of a class? A) void B) Object    C) no return type is allowed   D) int 40.    A class can have many different constructor methods as long as the names are different. A)true     B) false

  • An object is hanging by a string from the ceiling of an elevator. The elevator is moving upward with a constant speed....

    An object is hanging by a string from the ceiling of an elevator. The elevator is moving upward with a constant speed. What is the magnitude of the tension in the string? The magnitude of the tension in the string is equal to the magnitude of the weight of the object. The magnitude of the tension in the string is less than the magnitude of the weight of the object. The tension in the string cannot be determined without knowing...

  • What will be displayed when the sub procedure that follows is run, true or false? Show...

    What will be displayed when the sub procedure that follows is run, true or false? Show all the steps. Option Explicit Sub test() Dim x As Double, z As Double, a As String, b As String x = 7 z = 6 a = "b" b = "a" If Not z > 4 * Atn(1) Or rrr(x) < z Or x > 7 And a > b Then     MsgBox "true" Else     MsgBox "false" End If End Sub Function...

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • Give an object that could be represented through super and sub classes? Give an example and...

    Give an object that could be represented through super and sub classes? Give an example and describe what you think would be data member in the super class and then what would be data member in the subclass. Please create a UML diagram for one of the classes described. You can do this by inserting a table or list in the post or create it in Cacoo.

  • A string is attached to a ball and the ball is being swung in a horizontal...

    A string is attached to a ball and the ball is being swung in a horizontal circle. The string exerts a force on the ball. In what direction is the force always directed? While the string is attached to the ball and the ball moves in a horizontal circle at a constant speed, does the force change both the direction and speed of the ball? Does the force change both direction of travel and speed of the ball? Explain. An...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT