OOP Flashcards
(40 cards)
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.