OOP Flashcards
What features does OOP support?
The bare minimum is it must support Classes, Objects, Encapsulation, Inheritance, Polymorphism.
What is polymorphism?
When an object has many Is-A identities. Most common use is using a parent class reference to refer to child class object.
Give example of polymorphism?
Dog is an Animal
Dog is a Dog
Dog is a Carnivore
Dog is an object
Dog d = new Dog(); Animal a = d;
Give example of taking advantage of polymorphism?
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.
Advantage of using Polymorphism?
Useful for making lists - a list of animals could contain different sub animals.
Difference between class and interface?
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).
Difference between abstract method and interface?
Abstract class cannot be instantiated. Interface neither. Interface all abstract methods and static final fields.
When to choose interface over abstract class?
Choose abstract class if some default behavior shared amongst related classes. If providing common functionality to unrelated classes use interfaces.
What are access modifiers?
Private: class only. Package: class + package Protected: class, package + subclass Public: anybody