OOP Flashcards

1
Q

What features does OOP support?

A

The bare minimum is it must support Classes, Objects, Encapsulation, Inheritance, Polymorphism.

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

What is polymorphism?

A

When an object has many Is-A identities. Most common use is using a parent class reference to refer to child class object.

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

Give example of polymorphism?

A

Dog is an Animal
Dog is a Dog
Dog is a Carnivore
Dog is an object

Dog d = new Dog();
Animal a = d;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give example of taking advantage of polymorphism?

A
Animal a = new Dog();
a.noise();

Compiler uses noise() in animal to validate. At run time though JVM uses overridden noise() in the Dog class.

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

Advantage of using Polymorphism?

A

Useful for making lists - a list of animals could contain different sub animals.

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

Difference between class and interface?

A

A class is a blueprint. Must be instantiated into object. Interface is more like a contract, can not be instantiated. Interfaces implemented, only one class can be extended. Interface variables are constants (static and final).

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

Difference between abstract method and interface?

A

Abstract class cannot be instantiated. Interface neither. Interface all abstract methods and static final fields.

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

When to choose interface over abstract class?

A

Choose abstract class if some default behavior shared amongst related classes. If providing common functionality to unrelated classes use interfaces.

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

What are access modifiers?

A
Private: class only.
Package: class + package
Protected: class, package + subclass
Public: anybody
How well did you know this?
1
Not at all
2
3
4
5
Perfectly