WEEK 4 - INTRODUCTION TO ABSTRACT CLASSES, POLYMORPHISM AND INTERFACES Flashcards

1
Q

What is an abstract method?

A

A method with no implementation.

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

What are the rules for abstract methods?

A
  1. Declared with the keyword abstract
  2. Never private
  3. Ends with a semicolon. No method body and no curly brackets.

Example.
public abstract void paint();

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

How to guarantee abstract method implementation?

A

Abstract methods MUST be implemented in the subclasses (if not, in a subclass of the subclass).

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

What is the error thrown of an abstract method unsuccessfully implemented?

A

Syntax error.

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

What does polymorphism mean?

A

Polymorphism means “many forms” or many shapes.

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

Polymorphism can be applied to which three things?

A
  • Types (classes)
  • Methods
  • Generics (< >)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a polymorphic method indicate?

A

A polymorphic method indicates multiple “forms” or implementations.

This is alternatively known as dynamic binding.

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

When is the implementation of polymorphic methods?

A

During runtime.

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

Abstract methods are always what?

A

Polymorphic.

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

What is an interface?

A

An interface represents a “contract” or agreement.
In Java, the implementing class will define all methods of the interface.

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

What’s an interface similar to?

A

An interface is alike to an abstract class with all methods abstract.

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

Interface is a special type of class, what keyword is used instead of class?

A

interface.

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

What are the four rules for interfaces in Java?

A
  1. Contains only public method declarations, no method implementations.
  2. Can have field variables but they must be final
  3. Interface name is often as adjective
    - Names often end in “…able”, e.g. Comparable
  4. When using an interface, you write implements instead of extends
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the properties of interfaces?

A
  1. Similar to abstract classes, we can’t create instances (objects) from interfaces.
  2. An implementing class must implement all the interface methods (as if they were all abstract).
  3. A class can implement more than one interface.
  4. Many classes can implement the same interface without sharing the same class hierarchy.
  5. Methods stubs in the interface are never ‘private’.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the benefits and uses of interfaces?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between the keywords extends and inheritance?

A

A class can only extend one other class (have at most one superclass).

A class can implement many interfaces.