Una classe è un pezzo di codice all’interno di un programma che descrive un particolare tipo di oggetti
La classe fornisce un prototipo astratto gli oggetti di un particolare tipo, definendo la loro struttura in termini di:
- Campi (stato) degli oggetti
- Metodi (comportamenti) degli oggetti
Un oggetto è un’istanza (un esemplare) di una classeUn programma può creare e usare uno o più oggetti (istanze) della stessa classe
Class Structure
Create a class in Java
oss
- Ogni classe è memorizzata in un file separato
- Il nome del file DEVE essere lo stesso della classe, con estensione .java
We can create a class in Java using the class keyword. For example,
class ClassName {
// fields
// methods
}
Here, fields (variables) and methods represent the state and behaviour of the object respectively.
- fields are used to store data
- methods are used to perform some operations
For our bicycle object, we can create the class as
class Bicycle {
// state or field
private int gear = 5;
// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
}
Here, Bicycle is a prototype. Now, we can create any number of bicycles using the prototype. And, all the bicycles will share the fields and methods of the prototype.
Note
We have used keywords
private
andpublic
. These are known as access modifiers. To learn more, visit Java - Access Modifier