The following declaration and assignment produces an error.
List mylist1 = new List();
The error says, “List is abstract; Cannot be instantiated”. The following code is successful;
List mylist2 = new ArrayList();
The Oracle Java system documentation says that “List” is an interface and that ArrayList is a concrete implementation of List. The oracle documentation also notes that the ArrayList class has three additional methods that are not in the parent List interface
Considering the above information, in concise terms answer the following questions;
(a) What is the difference between an abstract class, and interface and a normal class?
(b)What are the two fundamental things that happen when the “new” command is used to instantiate an object.
(c) if you declare a list of type “List” but instantiate it as an “ArrayList”, will the three extra methods be available to be called via the mylist2 object?
(d) The following command may also be successfully used;
ArrayList mylist3 = new ArrayList();
If this object is used instead of mylist2 above will the three extra methods be available to be called? (e) Are all ArrayList arrays collections of strings? Expand on what the <> means?
The Correct Answer and Explanation is :
(a) Difference between an Abstract Class, an Interface, and a Normal Class:
- Abstract Class:
- An abstract class cannot be instantiated on its own (you cannot create objects of an abstract class).
- It can have both abstract methods (methods without implementations) and concrete methods (methods with implementations).
- Abstract classes can have member variables, constructors, and other methods that are not abstract.
- A class that inherits from an abstract class must implement all the abstract methods, unless it is also abstract.
- Interface:
- An interface is similar to an abstract class but only contains abstract methods (prior to Java 8). However, starting from Java 8, interfaces can contain default methods with implementations.
- All methods in an interface are implicitly public and abstract (unless marked as default).
- Interfaces do not have member variables; they can only have constants (static final variables).
- A class can implement multiple interfaces, making it more flexible than abstract classes (which support single inheritance).
- Normal Class:
- A normal class is a fully defined class with concrete methods that can be instantiated.
- It can have constructors, variables, and both concrete and abstract methods (though abstract methods require it to be abstract).
- A normal class is instantiated using the
newkeyword.
(b) Two Fundamental Things When the “New” Command is Used to Instantiate an Object:
- Memory Allocation: The
newcommand allocates memory for the object on the heap. - Constructor Call: The constructor of the class is called to initialize the object’s state.
(c) If You Declare a List of Type “List” but Instantiate It as an “ArrayList”, Will the Three Extra Methods Be Available?
No, the three extra methods in ArrayList will not be available to the mylist2 object when it is declared as a List and instantiated as an ArrayList. This is because List is the interface, which defines only the methods declared in it, and not the additional methods in ArrayList. The object will only be able to access methods from the List interface.
(d) If You Declare an ArrayList as ArrayList mylist3 = new ArrayList();, Will the Three Extra Methods Be Available to Be Called?
Yes, the three extra methods will be available to the mylist3 object because it is explicitly declared as an ArrayList, which contains those extra methods. When you directly use ArrayList as the reference type, you can access all the methods defined within ArrayList.
(e) Are All ArrayList Arrays Collections of Strings? Expand on What the <> Means:
No, not all ArrayList objects are collections of strings. The ArrayList class is a generic class, which means it can store elements of any type, not just strings. The <> is called the “diamond operator,” and it allows you to specify the type of elements the ArrayList will hold. For example, ArrayList<String> would store strings, while ArrayList<Integer> would store integers.
The <> specifies the type parameter (generic type) for the collection, ensuring that only objects of the specified type can be added to the list.
Image Representation:
I’ll generate a diagram showing the relationship between abstract classes, interfaces, and normal classes, along with a simple example of generics (<>).
Let me generate that image.
Here is a diagram illustrating the differences between abstract classes, interfaces, and normal classes in Java, as well as the use of generics with ArrayList. This should help clarify how each of these components works, and how the <> operator is used to define the type of elements in collections like ArrayList. Let me know if you’d like any further explanation or modifications!
