week05 Polymorphism&interface Flashcards

1
Q

What is polymorphism in Java?

A

(Polymorphism of different objects to respond uniquely to the same method call.)In Java, polymorphism allows one interface to be used for a general class of actions, and specific action is determined at runtime. It means that you can write code that works with the superclass or interface type (the general class), but when you call a method, the specific action (the method in the actual subclass that’s being referenced at runtime) is what gets executed. This allows different subclasses to provide their own behaviors for the same method calls, and Java determines which method implementation to run at runtime.

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

What is method overloading in Java?

A

Method overloading is the ability to create multiple methods of the same name with different implementations by varying the parameter list (by number, type, or both). It’s resolved at compile time, and is a form of static (compile-time) polymorphism.

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

What is the difference between static and dynamic types in Java?

A

Static type is the type of the reference, which is known at compile time. Dynamic type is the type of the actual object instance, which may only be known at runtime. Polymorphic method calls depend on the dynamic type for determining the actual method to execute.

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

What is static type checking?

A

Static type checking occurs at compile time where the compiler verifies that each operation is being performed on data types that are allowed by the specifications, preventing type errors.

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

What is early binding in Java?

A

Early binding (also known as static binding) is when the method to be called is determined at compile time. This is the case with final, private, and static methods, as well as overloaded methods.

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

What is late binding in Java?

A

Late binding (also known as dynamic binding) occurs at runtime. Java uses late binding for method calls when the method executed is determined by the dynamic type of the object, allowing for dynamic method dispatch and polymorphic behavior.

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

What is an interface in Java?

A

An interface in Java is an abstract type that is used to specify a behavior that classes must implement. It can contain constants, method signatures(no implementation details), default methods, and static methods.

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

What are the rules for protected access in Java?

A

Protected access allows members (methods or fields) to be accessed within the same package or by subclasses in any package, supporting inheritance by providing more visibility than private access while still restricting public access.

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

What is the differences between polymorphism and duck typing?

A

Polymorphism typically involves an “is-a” relationship and is enforced at compile time in statically typed languages. Duck typing does not require an “is-a” relationship and is realized at runtime in dynamically typed languages. Polymorphism relies on inheritance and interfaces, while duck typing relies on the actual implementation of the methods and properites.

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

Can static methods be overridden in Java?

A

No, static methods cannot be overridden but can be hidden. The subclass version hides, not overrides, the superclass version.

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

Why should methods called from constructors not be overridden?

A

To avoid unexpected behavior due to polymorphic method dispatch during object construction. Use final methods to prevent overriding.

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

What are abstract classes in Java?

A

Classes declared with the abstract keyword; cannot instantiate objects directly and are designed to be subclassed.

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

Why use abstract classes?

A

To provide a common design for subclasses to inherit, ensuring shared structure and behavior.

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

What are concrete classes?

A

Classes that can be instantiated; they implement all declared methods.

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

Why code with references to abstract superclasses?

A

Enhances modularity and flexibility by reducing dependency on specific subclass types.

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

What contract must concrete subclasses fulfill when extending an abstract class?

A

Must implement all of the superclass’s abstract methods.

17
Q

Can abstract classes have constructors?

A

Yes, for initialization and validation, invoked by subclass constructors with super().

18
Q

How do abstract classes support polymorphism?

A

By allowing subclass objects to be referenced through abstract class types.

19
Q

What does implementing an interface entail?

A

A class must provide concrete implementations for all methods declared in the interface.

20
Q

When to use an interface instead of an abstract class?

A

When there is no default implementation or when unrelated classes need to share a common method signature.

21
Q

What are default methods in interfaces?

A

Methods with a default implementation; allow classes to inherit new methods without breaking existing implementations.

22
Q

Can interfaces contain static methods?

A

Yes, for utility or helper methods related to the interface’s functionality.

23
Q

What is the purpose of the Comparable<T> and Comparator<T> interfaces?

A

To define natural ordering (Comparable) or custom sort criteria (Comparator) for objects.

24
Q

How do interfaces contribute to polymorphism?

A

Enable treating objects of classes implementing the same interface as objects of the interface type, supporting dynamic method invocation.

インターフェースは、異なるクラスが同じメソッドセットを実装することを可能にします。これにより、これらのクラスのオブジェクトを共通のインターフェース型を通じて操作できるようになります。これはポリモーフィズムをサポートし、インターフェースで定義されたメソッドを呼び出すことを可能にしますが、オブジェクトの正確なクラスを知る必要はありません。

25
Q

How do interfaces and abstract classes fit into a class hierarchy?

A

Interfaces define the API, abstract classes implement shared logic and variables, and concrete classes provide specific implementations.

26
Q

What is the risk of calling overridable methods inside a superclass constructor?

A

The subclass version of the method will be executed. If the subclass method depends on initialization performed in the subclass constructor, it can lead to errors or incorrect behavior since the subclass constructor has not yet run.

27
Q

What is a best practice to avoid issues when a superclass constructor calls a method that could be overridden?

A

The superclass constructor should only call final methods or private methods, as these cannot be overridden by subclasses.

28
Q

Why should a constructor avoid calling non-final methods?
and What does the ‘final’ keyword in a method declaration ensure?

A

Non-final methods can be overridden by subclasses, potentially leading to unpredictable behavior if the subclass’s state is not fully initialized when the methods are called.

The ‘final’ keyword ensures that the method cannot be overridden by any subclass, thus preserving its intended behavior when called from a constructor.

29
Q

What is the order of initialization in Java?

A
  1. Static Members Initialization: The process starts at the very top of the inheritance tree. First, it sets up all the static parts (like variables and blocks) of each class in order. It goes from the top class all the way down to the subclass, initializing static items as they appear in the class, then moves to the next class lower in the hierarchy.
  2. Superclass Instance Variables and Constructors: This part starts just like the first one, at the top of the inheritance chain. It sets up the instance variables with their starting values as they’re written in the class. Then, it runs the constructor, which might change those starting values. This step is done for every class, moving down from the superclass to the subclass.
  3. Final Step - Instantiating Class Initialization: Lastly, the focus shifts to the class that you’re actually creating an instance of. Its instance variables are set up first, and then its constructor is run. This last step makes sure the object is completely ready to go, finishing up the setup process that goes through the whole inheritance line.
30
Q

In Java, what does the constructor of a subclass do in relation to its superclass constructor?

A

A subclass constructor implicitly calls the no-argument constructor of its superclass unless it calls a specific superclass constructor using super(arguments). Object initialization is complete when the subclass constructor finishes executing.