Stone Hyrax
New Coder
A class is a blueprint for creating objects. It defines:
- Fields (variables) that store data.
- Methods (functions) that define behavior.
Java:
public class NumeClasa {
// Variabile (proprietăți ale obiectului)
int id;
String nume;
// Constructor
public NumeClasa(int id, String nume) {
this.id = id;
this.nume = nume;
}
// Metodă (funcție) a clasei
public void afiseazaDetalii() {
System.out.println("ID: " + id + ", Nume: " + nume);
}
}
- Car is a class with two fields: brand and speed.
- It has a method displayInfo() that prints the car's details.
- Accept parameters.
- Return a value.
- Be called multiple times.
Java:
class MathOperations {
// Method with parameters that returns a value
int add(int a, int b) {
return a + b;
}
}