week04 inheritande&exceptions Flashcards
(39 cards)
What is an array in Java?
An array is a fixed-size collection that can store multiple items of the same type. The size is set when the array is created and cannot change. Arrays are zero-indexed, which means the first element is at index 0.
True or false?
“You can change the size of an array after it is created.”
False. Once created, the size of a Java array cannot change. You must create a new array if you need a different size.
How do you find the number of elements in an array?
Use the length
property of the array. if array
is an array, array.length
gives you the number of elements.
What should you be careful about with arrays of objects?
An array of objects is initialized with null
for each element. You must assign an object to each position before using it. Accessing null
elements will cause a NullPointerException
.
True or false:
“System.arraycopy() creates a deep copy of an array.”
False. It creates a shallow copy, meaning it copies the references, not the actual objects.
What is a multi-dimensional array in Java?
It’s an array of arrays. You can imagine it is like a table with rows and columns.
What is an ArrayList in Java?
An ArrayList is a list that backed by an array. It can grow and shrink. It’s part of Java’s collections Framework and provides more functions than a simple array.
True or false:
“An ArrayList has a fixed size”
False. Unlike arrays, an ArrayList can change its size dynamically.
What are key differences between an ArrayList and an array?
ArrayList can change size, only stores objects, and has many methods. Arrays have fixed size, can store primitives, have no methods, and can be multi-dimensional.
When is it better to use an ArrayList?
Use ArrayList when you need a dynamic collection that chagnes size, when you’re storing objects, and when you want useful methods like add
, remove
, and contains
.
When should you use an array instead of an ArrayList?
Use an array when you know the collection size won’t change, need to store primitives, or don’t need the extra features of ArrayList.
How do you find the size of an array in Java?
Use the length
property. Example: arrayName.length
returns the size as an int
.
What can an array contain in Java?
A single kind of ‘thing’ - primitives, object of a class, but all must be the same type.
What is composition in OOP?
A ‘has-a’ relationship where a class includes object references as instance variables, indicating ownership
What is aggregation in OOP?
A specific ‘has-a’ relationship where a class contains a collection of objects, which can exist independently.
What is the difference between composition and aggregation in OOP?
Composition implies ownership and a strong relationship; without the containing object, the contained objects do not make sense. Aggregation allows contained objects to exist independently.
What does the “is-a” relationship signify in Java, and how is it implemented?
It signifies a class inheriting from another class, indicating a parent-child relationship. Implemented with the extends
keyword, where the subclass “is a” specialized version of the superclass.
What are the pillars of Object-Oriented Programming (OOP) in Java?
Encapsulation, information hiding, inheritance, polymorphism, and abstraction.
How does Java’s inheritance relate to the Object class?
Every class in Java inherits from the Object class, gaining access to methods like toString()
, equals()
, and hashCode()
.
What are the rules of inheritance in Java regarding superclass and subclass?
Java supports only single inheritance; a subclass can have only one superclass, ensuring a clear “is-a” relationship.
Can a superclass have multiple subclasses? What must each subclass represent?
Yes, a superclass can have any number of subclasses. Each subclass must be a specialized version of the superclass.
How should common attributes be managed in an inheritance hierarchy?
Common attributes should be declared in the superclass. Subclass constructors must pass these attributes to the superclass constructor using super()
.
Should instance variables in the superclass be duplicated in the subclass?
No, instance variables should not be duplicated. Subclasses inherit them, including private ones, but access private variables through public or protected methods.
How does inheritance support encapsulation in Java?
By grouping related instance variables and methods in a superclass, subclasses inherit and can use these, promoting independence and reducing coupling.