OOP Flashcards
What is the difference between a class and an object
A class is the blueprint to make objects. Objects are an instance of a class.
What are you four access modifiers
- Public
- Private
- Default
- Protected
Access Modifier: Public explain
Access level is everywhere
Access modifier: Private
Access only from within the class. Cannot be accessed outside of the class
Access modifier: Protected
Access from only within the package and outside the package through the child.
Access modifier: Default
Access level is only within the package. It cannot be accessed from outside the package
Abstract and Interface explained
Both are contracts.
Abstract class is a class that cannot be created and has at least one abstract method. Mixture of unique and generalized methods that your subclasses will inherit.
Interface: Just abstract methods, anything that implements the interface has to define the method. o You want to specify the behavior of a particular data type, but not concerned about who implements its behavior
What is a static method
A static method is a method that pertains to a class not an object. This allow the method to be called with an instance of an object. Can only call other static methods and static variables.
What is a singleton
ensure that only one instance of a class is created and provide global access point to the object
What are the main concepts of OOP
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
Explain Abstraction
each object should only expose a high-level mechanism for using it. The internal implementation details are hidden. It only reveals operations relevant for other objects
When would you use a static method
The code in the method is not dependent on instance creation and is not using any instance variable.
A particular piece of code is to be shared by all the instance methods.
The definition of the method should not be changed or overridden.
you are writing utility classes that should not be changed.
Explain Polymorphism
override/overloading meaning that you can have a method with the same name as the parent with different functionality
Explain Inheritance
Reuse common logic and extract the unique logic into a separate class. Child receives stuff from the parent
Overriding
replaces that the function inherted from the parent does