22.05 Classes and Objects
Classes and Objects
Overview
Classes and objects are the building blocks of object-oriented programming in Java.
Topics
- Defining Classes
- Creating Objects
- Constructors
- Methods and Fields
Examples
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void introduce() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
}
}
Person john = new Person("John", 25);
john.introduce();