OOP Principles Flashcards
What is the difference between an object and a class?
1.
- A class is like the blueprint and inside the class is where objects are created
- Objects are the variables of a class.
2.
-A class is only declared once.
- An object can be declared multiple times
3.
- A class doesn’t take any memory when created, but an object does.
What is the difference between a stack and a heap?
- A stack is where primitive value data is stored and a heap is where object data is stores.
- A stack stores the reference data of an object
What is encapsulation? Give an example?
Encapsulation is the process of protecting data in a class from being accessed by code outside of its class and also allows you to better organize data within classes. Usually by setting a variable to private and using getters and setters.
ex. If you have a class called Students that is responsible for keeping track of student information like, name and id you could set the variables to private so that the values can’t be changed outside of the class and can only be accessed through their getters and setters that way everything stays protected and organized.
What is the difference between a regular class and an abstract class?
A regular class can be instantiated and an abstract class cannot.
What is the difference between a abstract class and a interface? When would you use them?
An interface can have only abstract methods whereas a abstract class can have both abstract and non abstract methods.
Members in an interface are public by default while in an abstract class member can be public. private, etc.
Methods can’t be implemented in an interface, but in an abstract class methods can be implemented and not implemented.
Use:
Use interface when you want to define what a object DOES. When the class that implements the interface is planning to implement all the methods declared in the class.
Use a abstract class when you want to define what a object IS. When a class class shares a lot of common fields and methods but needs to user other access modifiers besides public.
What is a static modifier? What can static be added to?
Static modifiers makes something belong to the class so it is independent of any objects. You don’t need to make an instance of an object to use a static method. You can just call the class.
You can put the static modifier on variables, methods, and classes.