Chapter 10 Flashcards

(37 cards)

1
Q

What is an abstract class in Java?

A

A class that cannot be instantiated and is used as a base for subclasses. It often includes abstract methods with no body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can an abstract class be instantiated?

A

No, you cannot create objects of an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you declare an abstract class?

A

Using the abstract keyword in the class declaration, e.g., public abstract class Product {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can abstract classes have non-abstract methods?

A

Yes, abstract classes can include methods with full implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if a subclass doesn’t implement all but some abstract methods from its abstract parent?

A

Then the subclass must also be declared abstract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can abstract methods be static or final?

A

No, abstract methods cannot be static or final.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are abstract classes represented in UML?

A

They are shown in italics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Java interface?

A

A collection of abstract methods and constants that define a contract a class must implement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you instantiate an interface?

A

No, interfaces cannot be instantiated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What keyword is used to create an interface?

A

interface, e.g., public interface Doable {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you implement an interface in a class?

A

Using the implements keyword in the class declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Are interface methods public by default?

A

Yes, all methods in an interface are public by default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can a class implement multiple interfaces?

A

Yes, Java supports multiple interface implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the default modifiers for fields in an interface?

A

All fields are public static final by default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the Comparable interface?

A

It defines a method compareTo to allow object comparison for sorting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does compareTo return when two objects are equal?

A

It returns 0.

17
Q

What does compareTo return if the calling object is less than the other?

A

It returns a negative number.

18
Q

What does compareTo return if the calling object is greater than the other?

A

It returns a positive number.

19
Q

What Java method can sort a list of Comparable objects?

A

Collections.sort().

20
Q

When should you use an abstract class?

A

When classes share code or have protected/private fields or partial implementations.

21
Q

When should you use an interface?

A

When unrelated classes need to implement the same method signatures or you want multiple inheritance.

22
Q

What are differences between abstract classes and interfaces?

A

Abstract classes can have fields and non-abstract methods; interfaces only have public static final fields and abstract methods.

23
Q

What is polymorphism?

A

The ability of a reference variable to change behavior depending on the object it refers to.

24
Q

What are the two types of polymorphism in Java?

A

Compile-time (method overloading) and runtime (method overriding).

25
What is a polymorphic reference?
A reference that can refer to different object types over time.
26
How does Java determine which method to call in runtime polymorphism?
Based on the actual object type, not the reference type.
27
What is a widening conversion?
Assigning a subclass object to a superclass reference.
28
What is a narrowing conversion?
Casting a superclass object to a subclass reference.
29
How are abstract classes and methods shown in UML?
They are shown in italics.
30
On what type is an overridden method called?
The overridden method is called based on the object's actual type at runtime, not the reference type.
31
What does the JVM do in runtime polymorphism?
It dynamically determines which method to call based on the actual object type.
32
What is a widening conversion in Java?
Assigning a subclass object to a superclass reference; it's safe and implicit.
33
What is a narrowing conversion in Java?
Assigning a superclass object to a subclass reference using an explicit cast; potentially unsafe.
34
What happens when you use an interface reference to call methods?
The method called depends on the actual object the interface reference points to.
35
What are the core Java collection interfaces?
`List`, `Map`, `Set`, `Queue` and their subtypes like `ArrayList`, `HashMap`, `TreeSet`, etc.
36
How do you sort custom objects like Human by name length?
Implement `Comparable` and define `compareTo` to compare name lengths.
37
What does `Collections.sort()` require?
The list elements must implement the `Comparable` interface.