OOP Flashcards
What does a getter do?
It gets the value attributes of a class
What does a setter do?
It sets the value of a class
What does a constructor do?
It forces attributes on creation of an object of the class.
(method that is ran when an object is instantiated)
What is method overloading?
Same method but different parameter or data type
What is constructor overloading?
Normal overloading but with multiple contstructors instead of methods.
What is method overriding
It is basically updating an already existing function
How to use inheritance in Java?
Public class Lowerclass extends Superclass
and
in constructor
super(variable name)
What does adding abstact to a class do?
- The class cannot be instantiated( meaning you cant make an object from the class, but they can have sub classes which you can instantiate ( make an object)
Polymorphism
What does adding abstract to a method do?
It means that the method doesnt have a body (no code)
So you must use it in a subclass.
What is the difference between a singly, doubly and circularly linked list.
Singly: normal
Doubly: has next and previous
Circularly: The tail is connected to the head (the tail points to the head.
How to overload methods?
Same method name but different data type or different no. of parameters
How to override methods?
You type @Override
then give the method in the sub class new code.
What does super keyword do?
Super refers to the subclass’s super class (similar to .this)
attributes:
super(values to be passed from the superclass);
this.value in the sub class
methods:
super.methodname();
How to use encapsulation?
Use getter and setter
How to code encapsulation?
When creating the object from the class, you have to use the getter and setter methods instead of objectName.variableName
The constructor will also use the setter to give values.
Use private and protected
What is polymorphism
Basically method overiding or overloading (u can use both) with multiple different classes that inherit from the same superclass.
How to do exceptions?
Try = Code to test
Catch = Code to run if theres an error
catch(Exception e) {}
(add more types of catch exceptions for different type of errors or just put exception)
Finally = Code to run after the try and catch
Throw new = Makes an error happen (you have to give the error type)
What is an Array
An array is a data structure, which can store a fixed-size collection of elements of the same data type.
What is the syntax in Java for creating a 2D array?
int [ ] [ ] arrayName = new int [ 3 ] [ 3 ];
OR
int [ ] [ ] arrayName =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
How to traverse in 2D arrays in Java
for ( int i = 0; i < arrayName.length; i++) {
for (int j = 0; j < arrayName[i].length; i++) {
System.out.println(arrayName[ i ] [ j ]
What is a 2D array?
A 2D array is an array of arrays, where each element is itself an array. It is a collection of
elements arranged in rows and columns.
What are 2 uses of 2D arrays?
2D arrays are useful for representing matrices or grids.
What is a linked list?
A linked list is a linear data structure where elements are not stored contiguously in memory.
Instead, each element (node) contains a value and a reference to the next node in the
sequence.
What are the 3 types of linked lists?
singly linked, doubly linked, and circular linked lists.
What are the main methods for linked lists?
insert, delete, display. delete, replace, inserting at a given position
What are stacks?
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle
Stacks can be implemented using arrays or linked lists.
What are 4 uses of stacks
Stacks are used in various applications, such as expression evaluation, backtracking
algorithms, and undo/redo operations.
What are the main methods used for stacks?
Push, pop, is Empty, peek, size
What are Queues?
A queue is a data structure that follows the First In, First Out (FIFO) principle. It’s similar to standing in a line or queue at a store, where the first person to join the line is the first to be served.
What are 3 uses of queues?
Queues are commonly used in various computer algorithms and applications, such as job scheduling, task management, and breadth-first search algorithms.
What are the 4 types of queues?
Linear Queue, Circular Queue, Priority Queue
and Delay Queue.
What is a Linear Queue?
In a linear queue, elements are arranged in a linear or sequential manner. It follows the FIFO (First In, First Out) principle, where elements are inserted at one end (rear) and removed from the other end (front).
What is a Circular Queue?
Circular queues are similar to linear queues, but they wrap around when reaching the end of the underlying array or buffer. This allows efficient use of space and prevents wastage. Circular queues have a fixed size and offer better performance for certain applications like buffering in I/O operations.
What is a Priority Queue?
Priority queues are a type of queue where each element has an associated priority. Elements with higher priority are dequeued before those with lower priority, regardless of the order they were added. Priority queues are commonly used in algorithms like Dijkstra’s shortest path algorithm and in scheduling tasks.
What is a Delay Queue?
A delay queue holds elements until a certain delay has elapsed before allowing them to be dequeued. Elements are ordered based on their delay duration.
What is Encapsulation?
Encapsulation helps in achieving data hiding and protects the integrity of the data by preventing direct access from outside the class
Security
What is Abstraction
Abstraction is like using a TV remote without knowing how it works internally. It allows you to focus on what an object does, rather than how it does it. You deal with the essential characteristics of an object, ignoring the unnecessary details.
Abstraction, on the other hand, focuses on hiding the implementation details of an object and emphasizing only the essential features or behaviors relevant to the interaction with the object. It allows us to work with high-level concepts without worrying about the low-level details of how those concepts are implemented.
encapsulation sets boundaries and controls access to the inner workings
What is Inheritance?
Inheritance is like passing down traits from parents to children. It allows a new class to inherit properties and behaviors from an existing class, promoting code reusability and facilitating hierarchical relationships.
What is Runtime Polymorphism
Method overriding
What is