Topic 7.2: Working with Inheritance - Develop code that makes use of polymorphism develop code that overrides methods  differentiate between the type of a reference and the type of an object Flashcards

1
Q

What is upcasting, and when does it occur in Java?

A

This is the automatic and safe process of converting a subclass object to a superclass reference.

This occurs when you assign an object of a subclass to a reference variable of its superclass. It guarantees that all methods and properties of the superclass are present in the subclass.

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

What is casting in Java, and what are the two types of casting?

A

Casting in Java is the process of converting a reference variable from one type to another. The two types of casting are:

  • Upcasting: Converting a subclass object to a superclass reference (automatic and safe).
  • Downcasting: Converting a superclass reference to a subclass reference (explicit and potentially risky).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are abstract methods different from regular methods in an abstract class?

A

In an abstract class, abstract methods are declared without an implementation using the abstract keyword, while regular methods have implementations like any other class.

Subclasses that extend an abstract class must either provide implementations for all abstract methods or be declared as abstract themselves.

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

Why is it important to use the instanceof operator before performing downcasting in Java?

A

This step is crucial to avoid ClassCastException.

It ensures that the object is of the expected type before the cast is attempted, preventing potential runtime errors and ensuring safer code execution.

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

When should you use an abstract class, and when should you use an interface?

A
  • Use abstract classes when you want to provide a common base class with some shared implementation, and you expect subclasses to extend it to provide specific behavior.
  • Use interfaces when you want to define a contract that multiple unrelated classes should adhere to, promoting loose coupling and flexibility in design. Interfaces are useful for behaviors that are not common to all subclasses and allow classes to provide their own implementation of the interface methods as needed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why would you use an abstract class for a common behavior like making a sound in animals, and an interface for behaviors like flying that are not common to all animals?

A

Using an abstract class for common behavior (e.g., making a sound) allows for shared implementation among subclasses.

On the other hand, using an interface for behaviors like flying ensures that only relevant subclasses implement the behavior, avoiding redundant code in classes that don’t need it.

Interfaces allow classes to provide their own unique implementation, promoting flexibility and maintainability.

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

What is the purpose of the @Override annotation?

A

This is used when overriding a method to indicate that it is intended to override a superclass method.

It improves code readability and provides additional safety by generating an error if there is a mistake in the method signature.

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

What is polymorphism in object-oriented programming?

A

This allows objects of different classes to be treated uniformly based on a shared superclass or interface.

It enables a reference variable of a superclass type to hold an object of its subclass and invoke the appropriate overridden method in the subclass.

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

The rules for this are:

  • The method in the subclass must have the same method signature (name, return type, and parameter types) as the method in the superclass.
  • The access level of the subclass method must be the same or less restrictive than the access level of the superclass method.
A

What are the two rules for method overriding?

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

How can you distinguish between the type of a reference variable and the type of the object it points to in Java?

A

To distinguish between the type of a reference variable and the type of the object it points to, you examine the declaration of the reference variable to determine its type.

To identify the type of the object it points to, you look at the class used to instantiate the object.

Additionally, you can use the instanceof operator to verify the runtime type of an object, which checks if an object is an instance of a particular class or interface.

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

This operator in Java is used to check whether an object is an instance of a specific class or implements a particular interface.

It returns a boolean value (true or false) based on the compatibility of the object with the provided class or interface.

A

What is the purpose of the ` instanceof` operator in Java, and what does it return?

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

This is the explicit process of converting a superclass reference to a subclass reference.

It is considered risky because not all superclass objects can be safely treated as subclass objects. If you try to downcast an object to an incompatible subclass type, it will throw a ClassCastException at runtime.

A

What is downcasting, and why is it considered risky in Java?

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

In Java, what does the type of a reference variable determine when calling methods on an object?

A

The type of a reference variable determines the methods that can be called on the object it points to.

The Java compiler checks if the method is present in the reference variable’s type (class or interface) to determine whether the method call is valid at compile-time.

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

These in Java are classes that cannot be instantiated directly.

They serve as blueprints for other classes and are designed to be subclassed.

The purpose of these is to provide a common base with shared behavior and characteristics, while allowing specific implementations to be defined in the subclasses.

A

What are abstract classes in Java, and what is their purpose?

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

This is when a subclass provides its own implementation for a method already defined in its superclass.

This allows the subclass to customize the behavior of the inherited method to suit its specific needs.

A

What is method overriding in inheritance?

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

When calling methods on an object in Java, what does the object’s type define?

A

The object’s type defines the actual behavior of the methods. When a method is called on an object, the JVM determines the actual implementation of the method to execute based on the object’s type (class or subclass).

This is known as runtime polymorphism or dynamic method dispatch.

17
Q

How do interfaces promote abstraction in Java?

A

Interfaces promote a high level of abstraction, as they define what classes should do without specifying how they should do it.

By implementing an interface, a class agrees to fulfill the contract defined by the interface, allowing behavior from multiple sources through interface implementation.

18
Q

What is method overriding in inheritance?

A

This is when a subclass provides its own implementation for a method already defined in its superclass.

This allows the subclass to customize the behavior of the inherited method to suit its specific needs.

19
Q

What are abstract classes in Java, and what is their purpose?

A

These in Java are classes that cannot be instantiated directly.

They serve as blueprints for other classes and are designed to be subclassed.

The purpose of these is to provide a common base with shared behavior and characteristics, while allowing specific implementations to be defined in the subclasses.

20
Q

This is used when overriding a method to indicate that it is intended to override a superclass method.

It improves code readability and provides additional safety by generating an error if there is a mistake in the method signature.

A

What is the purpose of the @Override annotation?

21
Q

How can polymorphism lead to more flexible and maintainable code?

A

By programming to interfaces or superclasses and leveraging polymorphism, code becomes more flexible and maintainable.

Instead of using specific class types, code interacts with objects through their common superclass or interface, enabling easy code extension and adaptation without modifying existing code.

22
Q

What are the differences between abstract classes and interfaces in Java?

A
  • Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.
  • Abstract classes can have instance variables, constructors, and regular methods with implementations. Interfaces can only have constants (static final variables) and method signatures.
  • A class can extend only one abstract class, but it can implement multiple interfaces.
23
Q

This is the automatic and safe process of converting a subclass object to a superclass reference.

This occurs when you assign an object of a subclass to a reference variable of its superclass. It guarantees that all methods and properties of the superclass are present in the subclass.

A

What is upcasting, and when does it occur in Java?

24
Q

What is downcasting, and why is it considered risky in Java?

A

This is the explicit process of converting a superclass reference to a subclass reference.

It is considered risky because not all superclass objects can be safely treated as subclass objects. If you try to downcast an object to an incompatible subclass type, it will throw a ClassCastException at runtime.

25
Q

This in Java is a reference type that defines a contract or a set of abstract methods that must be implemented by any class that implements it.

Unlike abstract classes, these cannot have any method implementations or instance variables; they only contain method signatures.

A

What is an interface in Java, and what does it define?

26
Q

This step is crucial to avoid ClassCastException.

It ensures that the object is of the expected type before the cast is attempted, preventing potential runtime errors and ensuring safer code execution.

A

Why is it important to use the instanceof operator before performing downcasting in Java?

27
Q

What is an interface in Java, and what does it define?

A

This in Java is a reference type that defines a contract or a set of abstract methods that must be implemented by any class that implements it.

Unlike abstract classes, these cannot have any method implementations or instance variables; they only contain method signatures.

28
Q

This operator in Java is used to determine if an object belongs to a specific type or its subclass. It helps verify the runtime type of an object, allowing you to check if an object is an instance of a particular class or interface.

A

What is the purpose of the instanceof operator in Java?

29
Q

What is the purpose of the instanceof operator in Java?

A

This operator in Java is used to determine if an object belongs to a specific type or its subclass. It helps verify the runtime type of an object, allowing you to check if an object is an instance of a particular class or interface.

30
Q

What are the two rules for method overriding?

A

The rules for this are:

  • The method in the subclass must have the same method signature (name, return type, and parameter types) as the method in the superclass.
  • The access level of the subclass method must be the same or less restrictive than the access level of the superclass method.
31
Q

This allows objects of different classes to be treated uniformly based on a shared superclass or interface.

It enables a reference variable of a superclass type to hold an object of its subclass and invoke the appropriate overridden method in the subclass.

A

What is polymorphism in object-oriented programming?

32
Q

What is the difference between the type of a reference and the type of an object it points to in Java?

A

The “type” of a reference refers to the class it is declared with, while the “type” of an object refers to the actual class of the instantiated object that the reference points to.

Example:

// Declaration of reference variable
Vehicle vehicleRef; // The type of vehicleRef is "Vehicle"

// Instantiating objects
vehicleRef = new Car(); // The object type is "Car"
33
Q

What is the purpose of the ` instanceof` operator in Java, and what does it return?

A

This operator in Java is used to check whether an object is an instance of a specific class or implements a particular interface.

It returns a boolean value (true or false) based on the compatibility of the object with the provided class or interface.