Java Flashcards
What is Object Oriented Programming (OOPs)?
Is a way of organizing the code by using object-oriented principles. It models real world entities as objects which encapsulate data and behavior. Is objects talking to each other.
method
Is a building block of a object oriented program. Blueprint of definining the behavior of an object. Like what a character is able to do in a video game.
What is a class ?
Is a building block of a object oriented program. Blueprint of objects having common properties. Like a blue print of a character
Object
is an instance of a class. Is what the class is building – in a video game is basically a character
Method
Is a building block of a object oriented program. Blueprint of definining the behavior of an object. Like what a character is able to do in a video game.
Interface
It serves as a blueprint for classes. s a contract that defines a set of methods or properties that a class must implement. That cannot be instated.
// Interface declaration
interface Animal {
// Method signature
void makeSound();
// Constant String TYPE = "Animal"; }
// Class implementing the interface
class Dog implements Animal {
// Implementing the method from the interface
public void makeSound() {
System.out.println(“Dog barks”);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Output: Dog barks
System.out.println(Animal.TYPE); // Output: Animal
}
}
abstraction
is a a blueprint for classes that cannot be instantiated directly
Encapsulation
is the bundling of data that operates into a single unit called a class. It includes
1. Data Hiding
2.Public interface
3. Data validation
// Class definition
public class Car {
// Fields
private String model;
private int year;
// Getter for model public String getModel() { return model; } // Setter for model public void setModel(String model) { this.model = model; } // Getter for year public int getYear() { return year; } // Setter for year public void setYear(int year) { this.year = year; } }
Polymorphism
It means that different objects can use the same method or function name, but each one can act in its unique way.
1. Method overriding-it occurs when a class has multiple methods with the same name but different parameters.
class Animal {
public void makeSound() {
System.out.println(“Animal makes a sound”);
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println(“Dog barks”);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Polymorphism
animal.makeSound(); // Output: Dog barks
}
}
- Method Overloading occurs when a class has multiple methods with the same name but different parameters
class Calculator {
public int add(int a, int b) {
return a + b;
}public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum1 = calculator.add(2, 3); // Invokes the int version of add()
double sum2 = calculator.add(2.5, 3.5); // Invokes the double version of add()
}
}
Inheritance
It’s passing down traits from the parent class to child class.
JDK
JAVA develoment kit it contains the compiler and debugger and JRE
JRE
Java run time environment it contains the libraries and the JVM
What is spring boot
Is an open source Java framework
Overloading
it occurs when a class has multiple methods with the same name but different parameters.
overriding
occurs when a class has multiple methods with the same name but different parameters