OOP Flashcards
What is OOP
OOP is a programming paradigm centered around the concept of objects.
What are the 4 Concepts of OOP?
Encapsulation
Inheritance
Polymorphism
Abstraction
What is Encapsulation
Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
What is Inheritance
Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class.
What is Polymorphism
Polymorphism in programming gives a program the ability to redefine methods for derived classes.
What is Abstraction
Through the process of abstraction, a programmer only puts relevant data about an object in order to reduce complexity and increase efficiency.
What are the 3 Java access modifiers?
Protected
\+ Public - Private # Protected
What does static and final do?
The static keyword is used to represent the class member. It is basically used with methods and variables to indicate that it is a part of the class, not the object.
static = global
final = constant
What is the class syntax?
public/private(access modifier)
class (keyword)
className{ codeblock }
public class Main { int x = 5; }
What is the variable/attribute syntax?
public/private(access modifier)
datatype (keyword)
value
private int x = 5;
What is the parameters syntax?
( datatype datavalue )
What is the method syntax?
public/private(access modifier)
return type (void or any other datatype)
methodName(parameters)
{codeblock}
What are the Java data types?
String
int
float
double
char
boolean
What are the 2 types of constructors?
Default Constructor
public class Person { private String name; private int age; // Normal constructor public Person() { this.name = "Unknown"; this.age = 0; } // Getter method for name attribute public String getName() { return name; } // Getter method for age attribute public int getAge() { return age; } } public class Main { public static void main(String[] args) { // Creating an object of Person using the default constructor Person person = new Person(); // Getting the name and age attributes using getter methods System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } }
Parameterized Constructor
`public class Person {
private String name;
// Parameterized constructor public Person(String name) { this.name = name; } // Getter method for name attribute public String getName() { return name; } }
public class Main {
public static void main(String[] args) {
// Creating an object of Person with a parameterized constructor
Person person = new Person(“John”);
// Getting the name attribute using getter System.out.println("Name: " + person.getName()); } } `
What is the constructor syntax?
public/private(access modifier)
constructorName (Which would be the class name)
classAttributes = initial value;
public Person( ) { this.name = "Unknown"; this.age = 0; }
or
public Person(String name) { this.name = name; this.age = age }
What is the getter syntax?
public/private(access modifier)
getterName()
{return attribute}
What is the setter syntax?
public/private(access modifier)
void (return type)
setterName()
(parameters)
{this.attributename = parametervalue }
public void setName(String newName) {
this.name = newName;
}
How to get user input?
import java.util.Scanner;
// Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } }
What is the Array syntax?
datatype [ ] arrayName = { value1, value2, value3 }
How to loop through an array?
String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
OR
String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
for (String i : cars) {
System.out.println(i);
}
What is the 2D array syntax?
datatype [ ] arrayName = { {value1, value2, value3} //array 1 {value4, value5, value6} //array 2 }
How to loop through a 2D array?
public class Main { public static void main(String[ ] args) { int[ ][ ] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; for (int i = 0; i < myNumbers.length; ++i) { for(int j = 0; j < myNumbers[i].length; ++j) { System.out.println(myNumbers[i][j]); } } } }
Remember this
What is the class diagram structure?
Three parts
Class Name
Attributes
Methods
What are the 3 types of access modifiers and their symbols?
Class Diagrams
Protected
\+ Public - Private