CS2400 M2 Flashcards
What is a Class vs Object
A class specifies the data that objects made from it have, and what actions they may perform. An object is an instance of a class.
What is OOP?
Object Oriented Programming: A program that is represented by objects that interact with one another with various actions, as opposed to procedural code.
Why would you define a variable as Private vs Public
It is for abstraction purposes, and they provide the ability to control the level of security of the variable.
What does each access modifier do?
Default: Only accessible within the package.
Private: Only accessible by methods in-class.
Protected: Only accessible within the package, and subclasses.
Public: No restrictions.
What is Abstract modifier?
Use-modifier that has no definition in the parent class and must be overridden by ALL child classes. Used in polymorphism.
What is Final modifier?
Use-modifier that is defined in the parent class, and may not be overridden by child classes.
What is Static modifier?
Shared by all instances of the class once defined. It works as global data. Ex: counter, constants, main method, methods to use in main.
Difference between call by Value vs Reference
Value: passing in a primitive value will create a COPY and then save it to the object or method.
Reference: passing in the identical reference to another object.
What is the structure of a method?
The signature/header in the first line, including modifiers, return type, name and parameters. The body is enclosed in curly braces and defines the method itself.
Guidelines for calling super();
Default super(); constructor is called automatically. If needed, a different super constructor must be called before making any other definitions.
How do you differentiate the child’s constructors from its parent’s?
Child uses this();
Parent uses super();
What does each use modifier do?
Abstract: Forces the child classes to re-define.
Final: Does not allow for the child classes to re-define..
Static: Allows all instances to share.
*What is Overloading?
Overloading: methods in the same class have the same name but different signature. (compiler changes the name). “Grammatical Sugar”
*What is Overriding?
Overriding: a method in the child class has an identical signature to the same method in the parent class. Happens at runtime, and more resource intensive. “Runtime Magic”
How come we can declare child as parent, but not vice versa?
Ex. 1: Parent thing = new Child();
This is fine because the parent does not have any fields that the child constructor cannot handle.
Ex. 2: Child thing = new Parent();
This does not work because the child needs to use fields that the parent constructor can’t handle’