Chapter 6: Class design Flashcards

1
Q

Override happens in runtime and can be used do polymorphism in runtime.

A

True

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

Overload altera a assinatura do método e ocorre dentro da mesma classe.

A

Verdadeiro

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

Override pode ser usado em métodos construtores.

A

Falso

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

Overload permite o uso de diferentes assinaturas para métodos com o mesmo nome e pode ser aplicado em métodos construtores.

A

Verdadeiro

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

Override ocorre dentro da mesma classe e requer alteração de assinatura.

A

Falso

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

True or False: In Java 17, a method inside an abstract class must be marked as abstract if it does not have a method body. If false, why?

A

True

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

An abstract class in Java 17 can contain both abstract and non-abstract methods. If false, why?

A

True

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

A non-abstract method inside an abstract class must have a method body in Java 17. If false, why?

A

True

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

Java 17 allows abstract classes to define methods without a body, even if they are not marked abstract. If false, why?

A

False: A method without a body must be marked abstract.

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

If an abstract method is declared inside an abstract class in Java 17, it must be implemented by its first concrete subclass. If false, why?

A

True

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

True or False: In Java 17, a subclass automatically includes all members of the superclass, including private members. If false, why?

A

False: Private members are not inherited; they are only accessible within the superclass.

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

A subclass in Java 17 must always explicitly declare the extends keyword to inherit from a superclass. If false, why?

A

True

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

A superclass in Java 17 cannot be marked final if it is meant to be inherited. If false, why?

A

True

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

In Java 17, a subclass can inherit both instance variables and methods from its superclass. If false, why?

A

True

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

Inheritance in Java 17 is not transitive, meaning a subclass does not inherit from its grandparent class. If false, why?

A

False: Inheritance is transitive; a subclass indirectly inherits from all ancestor classes.

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

A class can extend multiple classes in Java 17 using multiple extends keywords. If false, why?

A

False: Java does not support multiple inheritance with classes; a class can extend only one superclass.

17
Q

Protected members of a superclass are accessible in the subclass even if they are in different packages. If false, why?

18
Q

If a superclass and subclass are in the same package, the subclass can access package-private (default) members. If false, why?

19
Q

The term “supertype” in Java 17 applies only to classes, not interfaces. If false, why?

A

False: “Supertype” applies to both classes and interfaces.

20
Q

Private members of a superclass can never be accessed in a subclass, even through methods of the superclass. If false, why?

A

False: A subclass can access private members indirectly through public or protected methods of the superclass.

21
Q

What is Liskov Substitution Principle and Type Safety?

A

The rule exists to ensure that objects of a subclass can be used wherever objects of the superclass (or interface) are expected — without breaking access expectations.

22
Q

Can an overridden method have more restrictive access than the method in the superclass or interface?

A

No, an overridden method cannot have more restrictive access than the original method. It must be at least as accessible.

23
Q

What happens if a subclass tries to override a public method from its superclass with a protected or private method?

A

A compile-time error occurs because the overridden method must maintain or increase visibility, not reduce it.

24
Q

In object-oriented programming, what determines which overridden method is called at runtime?

A

The actual type of the object, not the reference type, determines which overridden method is executed.

25
Q

If a method is overridden in a subclass, what happens when the method is called on a reference of a superclass type?

A

The subclass’s version of the method is executed, thanks to polymorphism and dynamic method dispatch.

26
Q

Why does calling a method on a base type reference sometimes produce output from a subclass implementation?

A

Because the method is overridden in the subclass, and the object at runtime belongs to the subclass, not the base class.