week05 Polymorphism&interface Flashcards
What is polymorphism in Java?
(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.
What is method overloading in Java?
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.
What is the difference between static and dynamic types in Java?
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.
What is static type checking?
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.
What is early binding in Java?
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.
What is late binding in Java?
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.
What is an interface in Java?
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.
What are the rules for protected access in Java?
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.
What is the differences between polymorphism and duck typing?
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.
Can static methods be overridden in Java?
No, static methods cannot be overridden but can be hidden. The subclass version hides, not overrides, the superclass version.
Why should methods called from constructors not be overridden?
To avoid unexpected behavior due to polymorphic method dispatch during object construction. Use final methods to prevent overriding.
What are abstract classes in Java?
Classes declared with the abstract
keyword; cannot instantiate objects directly and are designed to be subclassed.
Why use abstract classes?
To provide a common design for subclasses to inherit, ensuring shared structure and behavior.
What are concrete classes?
Classes that can be instantiated; they implement all declared methods.
Why code with references to abstract superclasses?
Enhances modularity and flexibility by reducing dependency on specific subclass types.