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
What is upcasting, and when does it occur in Java?
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.
What is casting in Java, and what are the two types of casting?
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 are abstract methods different from regular methods in an abstract class?
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.
Why is it important to use the instanceof operator before performing downcasting in Java?
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.
When should you use an abstract class, and when should you use an interface?
- 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.
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?
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.
What is the purpose of the @Override annotation?
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.
What is polymorphism in object-oriented programming?
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.
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.
What are the two rules for method overriding?
How can you distinguish between the type of a reference variable and the type of the object it points to in Java?
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.
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.
What is the purpose of the ` instanceof` operator in Java, and what does it return?
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.
What is downcasting, and why is it considered risky in Java?
In Java, what does the type of a reference variable determine when calling methods on an object?
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.
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.
What are abstract classes in Java, and what is their purpose?
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.
What is method overriding in inheritance?