Quiz: Java polymorphism and interfaces
This quiz covers key concepts of Java polymorphism and interfaces, focusing on definitions, examples, and practical applications to enhance understanding.
Quiz(48 questions)
1. What is the primary purpose of an abstract class in Java?
Terms in this Study Set(48)
Basics of Polymorphism(16)
What is polymorphism in Java?
Polymorphism allows methods to do different things based on the object that it is acting upon. It includes method overriding and overloading.
Define method overriding.
Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. - Must have the same name and parameters.
True or False: Overriding can change the return type.
False. The return type must be the same or a subtype of the return type in the superclass method.
Fill in the blank: Method ________ allows a class to have multiple methods with the same name.
Overloading. - Method signature must differ in parameters.
What is the difference between overriding and overloading?
Overriding: Same method name, same parameters, different class. Overloading: Same method name, different parameters, same class.
Cause → Effect: What happens when a subclass overrides a method?
The subclass's version is executed, allowing custom behavior while still adhering to the superclass contract.
Example of method overloading.
Consider: void add(int a, int b) void add(double a, double b) Both named 'add' but differing parameters.
What is dynamic method dispatch?
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime rather than compile-time.
True or False: A subclass can override a private method.
False. Private methods are not visible to subclasses and cannot be overridden.
List benefits of polymorphism.
- Code reusability - Flexibility - Maintainability - Interchangeability of objects
What is the purpose of the 'super' keyword?
'Super' is used to call the superclass's method or constructor, enabling access to overridden methods or parent constructors.
How does Java determine which method to execute?
Java uses the object's runtime type to determine which overridden method to execute, enabling dynamic behavior.
Example of polymorphism in action.
Consider a superclass 'Animal' with a method 'sound()'. Subclasses 'Dog' and 'Cat' override 'sound()' to bark and meow, respectively.
What is compile-time polymorphism?
Compile-time polymorphism occurs when method overloading is resolved at compile time, based on the method signature.
Fill in the blank: Overloaded methods must have different ________.
Parameter lists. - This includes the number or types of parameters.
True or False: Polymorphism is only applicable to methods.
False. Polymorphism can apply to methods and to objects, enabling different behaviors and functions.
Understanding Interfaces(16)
What is an interface in Java?
An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain instance fields or constructors.
Why use interfaces?
Interfaces provide a way to achieve abstraction and multiple inheritance in Java. They enable classes to implement multiple behaviors, ensuring a common protocol for different objects.
How to declare an interface?
To declare an interface, use the 'interface' keyword. Example: ```java interface Vehicle { void drive(); } ```
True or False: Interfaces can have method implementations.
False. Interfaces can have default methods with implementations but traditionally only declare method signatures.
Fill in the blank: A class implements an interface using the ______ keyword.
implements
What does polymorphism allow with interfaces?
Polymorphism allows objects to be treated as instances of their interface type rather than their actual class type, which enables flexibility and dynamic method resolution.
Compare interface and abstract class.
Interface: - No concrete methods (except defaults) - Multiple inheritance Abstract Class: - Can have concrete methods - Single inheritance only
How to implement an interface?
A class implements an interface by providing concrete implementations for all of its methods. Example: ```java class Car implements Vehicle { public void drive() { System.out.println("Driving a car"); } } ```
What is a default method in an interface?
A default method allows an interface to provide a default implementation for a method. This helps in extending interfaces without breaking existing implementations.
True or False: A class can implement multiple interfaces.
True. A class in Java can implement multiple interfaces, allowing it to inherit behaviors from different sources.
What are functional interfaces?
Functional interfaces are interfaces with exactly one abstract method, enabling lambda expressions. Example: ```java @FunctionalInterface interface Calculator { int add(int a, int b); } ```
Cause → Effect: Implementing an interface affects class behavior how?
It forces the class to provide implementations for the interface's methods, ensuring a consistent contract.
How to use an interface in a method parameter?
You can use an interface as a parameter type to create methods that can accept any implementing class. Example: ```java void startVehicle(Vehicle v) { v.drive(); } ```
What happens if a class does not implement all interface methods?
If a class does not implement all methods of an interface, it must be declared abstract, and cannot be instantiated.
Fill in the blank: An interface can extend another ______.
interface
What is the purpose of marker interfaces?
Marker interfaces, like Serializable, are used to provide metadata to classes without defining methods, signaling certain properties to the Java runtime.
Advanced Polymorphism Concepts(16)
What is an abstract class?
An abstract class cannot be instantiated and may contain abstract methods. - Base for subclasses - Can have fields and methods - Supports polymorphism
True or False: Java allows multiple inheritance with classes.
False. Java does not support multiple inheritance with classes to avoid ambiguity, but it allows multiple inheritance through interfaces.
How does an abstract class differ from an interface?
Abstract class: can have method implementations, fields; Interface: only method signatures before Java 8, default methods allowed now.
What happens if a subclass does not implement all abstract methods?
The subclass must be declared abstract. It cannot be instantiated until all abstract methods are implemented.
Fill in the blank: Abstract classes can define ______ methods.
Both abstract and concrete methods.
Use case for interfaces?
To define a contract for classes without forcing a class hierarchy. - Achieves multiple inheritance - Promotes code flexibility
True or False: Interfaces can have state (fields).
False. Interfaces cannot have instance fields; all fields are implicitly public, static, and final.
How does Java handle polymorphism with interfaces?
Classes implementing the same interface can be used interchangeably, allowing for dynamic method binding at runtime.
What is a common problem with multiple inheritance?
The Diamond Problem: Ambiguities occur when a subclass inherits from two classes with conflicting method implementations.
Example of polymorphism with an abstract class.
Consider an abstract class `Shape` with a method `draw()`. Subclasses like `Circle` and `Square` implement `draw()` differently.
Comparison: abstract class vs interface.
Abstract class: can contain state and implemented methods; Interface: purely a contract, no state.
Cause: A class implements multiple interfaces.
Effect: The class inherits the abstract methods of all interfaces and must provide implementations.
What is the purpose of using abstract classes?
To provide a common base while allowing subclasses to customize specific behaviors and methods.
True or False: Abstract classes can be final.
False. Abstract classes cannot be declared final, as they need to be subclassed.
What is the main advantage of polymorphism?
It allows for code flexibility and reusability by enabling objects to be treated as instances of their parent class or interface.
Fill in the blank: Polymorphism enables ______ code.
More maintainable and flexible code.
Questions in this Study Set(48)
1. What is the primary purpose of an abstract class in Java?
2. What does polymorphism allow in Java programming?
3. What keyword is used to declare an interface in Java?
4. Which of the following statements about interfaces is true?
5. What is method overriding?
6. Which statement about interfaces is FALSE?
7. What must a subclass do if it does not provide implementations for all abstract methods?
8. True or False: The return type of an overridden method can be different from the superclass method.
9. What is one primary benefit of using interfaces in Java?
10. Which of the following is NOT a feature of interfaces?
11. What does method overloading refer to?
12. In the context of interfaces, what does the term 'implementation' refer to?
13. How does polymorphism enhance code flexibility?
14. Which of the following statements correctly differentiates overriding and overloading?
15. Which of the following is an example of a functional interface?
16. Fill in the blank: An abstract class can define both ______ methods.
17. What happens when a subclass overrides a method from its superclass?
18. How does polymorphism relate to interfaces?
19. Which scenario illustrates polymorphism using an abstract class?
20. Which of the following is an example of method overloading?
21. If a class does not provide implementations for all methods of an interface, what must it be declared as?
22. What happens when a class implements multiple interfaces?
23. What is dynamic method dispatch?
24. What does the 'implements' keyword do in Java?
25. True or False: An abstract class can be instantiated directly.
26. True or False: A subclass can override a private method of its superclass.
27. What is the primary purpose of a marker interface?
28. What is the main drawback of multiple inheritance in Java?
29. Which of the following is NOT a benefit of polymorphism?
30. Which of the following can NOT be included in an interface?
31. How do interfaces support dynamic method binding?
32. What is the purpose of the 'super' keyword in Java?
33. In what way can an interface be used in method parameters?
34. Fill in the blank: An abstract class cannot be marked as ______.
35. How does Java determine which method to execute when polymorphism is involved?
36. How can a class inherit from multiple interfaces?
37. Which statement correctly compares an abstract class and an interface?
38. What is compile-time polymorphism?
39. What is the relationship between an interface and an abstract class in Java?
40. What is the purpose of using interfaces in Java?
41. Fill in the blank: Overloaded methods must have different ________.
42. Which keyword is used to extend an interface?
43. True or False: Polymorphism can lead to more maintainable code.
44. True or False: Polymorphism is applicable only to methods in Java.
45. What happens if an interface is modified to add a new method?
46. Which of the following statements correctly describes a difference between an abstract class and an interface in Java?
47. Which statement best describes dynamic method dispatch in Java?
48. Which of the following statements about a default method in an interface is TRUE?
Related Study Sets
Abitur Rekursion
Abitur: Abitur Klassen und Objekte
Was ist ein Algorithmus Schritt für Schritt
if und Schleifen Notizen
Wiederholung: Funktionen
Test: Binärzahlen
Listen Notizen
Schleife Alltag Beispiel Begriffe
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

