WEEK 4 - INTRODUCTION TO ABSTRACT CLASSES, POLYMORPHISM AND INTERFACES Flashcards
What is an abstract method?
A method with no implementation.
What are the rules for abstract methods?
- Declared with the keyword abstract
- Never private
- Ends with a semicolon. No method body and no curly brackets.
Example.
public abstract void paint();
How to guarantee abstract method implementation?
Abstract methods MUST be implemented in the subclasses (if not, in a subclass of the subclass).
What is the error thrown of an abstract method unsuccessfully implemented?
Syntax error.
What does polymorphism mean?
Polymorphism means “many forms” or many shapes.
Polymorphism can be applied to which three things?
- Types (classes)
- Methods
- Generics (< >)
What does a polymorphic method indicate?
A polymorphic method indicates multiple “forms” or implementations.
This is alternatively known as dynamic binding.
When is the implementation of polymorphic methods?
During runtime.
Abstract methods are always what?
Polymorphic.
What is an interface?
An interface represents a “contract” or agreement.
In Java, the implementing class will define all methods of the interface.
What’s an interface similar to?
An interface is alike to an abstract class with all methods abstract.
Interface is a special type of class, what keyword is used instead of class?
interface.
What are the four rules for interfaces in Java?
- Contains only public method declarations, no method implementations.
- Can have field variables but they must be final
- Interface name is often as adjective
- Names often end in “…able”, e.g. Comparable - When using an interface, you write implements instead of extends
What are the properties of interfaces?
- Similar to abstract classes, we can’t create instances (objects) from interfaces.
- An implementing class must implement all the interface methods (as if they were all abstract).
- A class can implement more than one interface.
- Many classes can implement the same interface without sharing the same class hierarchy.
- Methods stubs in the interface are never ‘private’.
What are the benefits and uses of interfaces?
- Can help achieve the object-oriented concept of encapsulation.
- Can be used to define an API (Application Programmer Interface).
- Commonly used to implement event handlers and callbacks.
- May be used to stimulate multiple inheritance in Java.