Java Objects and Classes
Java is an Object-Oriented Programming (OOP) language. This means programs are built around "objects". To understand objects, we must first understand "classes". This chapter will explore these two fundamental concepts in depth.
What is a Class?
Think of a class as a blueprint or template. It describes the attributes (characteristics) and behaviors (what it can do) that a type of thing has in common.
For example, we can create a Dog class. This blueprint would define the attributes and behaviors that all dogs have:
- Attributes (State):
String breedint ageString color
- Behaviors (Methods):
bark()wagTail()eat()
A class itself is not a concrete entity; it's just a definition. Like architectural blueprints for a house, the blueprint itself is not a house, but it details how to build one.
What is an Object?
An object is a concrete instance created from a class blueprint. If the Dog class is the blueprint for dogs, then a specific, 3-year-old brown Labrador named "Buddy" is a Dog object.
- This "Buddy" object has all the attributes defined by the
Dogclass, with specific values (breed=Labrador, age=3, color=brown). - It can also perform all the behaviors defined by the
Dogclass (bark(),wagTail(), etc.).
You can create thousands of different Dog objects from the same Dog class blueprint, each with its own unique attribute values.
Defining a Class in Java
Below is how to define the Dog class we discussed above in Java. The code is typically saved in a file with the same name as the class, i.e., Dog.java.
// Filename: Dog.java
public class Dog {
// Instance Variables - define object attributes
String breed;
int age;
String color;
// Methods - define object behaviors
void bark() {
System.out.println("Woof!");
}
void wagTail() {
System.out.println("Dog is wagging its tail...");
}
void displayInfo() {
System.out.println("Breed: " + breed + ", Age: " + age + ", Color: " + color);
}
}- Instance Variables:
breed,age,color- these variables declared in the class but outside methods define the object's attributes. Each object instance has its own copy of instance variables. - Methods:
bark(),wagTail(),displayInfo()- these are functions defined within the class that describe operations the object can perform.
Creating and Using Objects in Java
Once we have a class blueprint, we can create and use its objects in another class (for example, one containing the main method).
// Filename: Main.java
public class Main {
public static void main(String[] args) {
// 1. Create object (Instantiation)
// Use 'new' keyword and class constructor to create a new Dog object
Dog myDog = new Dog();
// 2. Access and set attributes
// Use the dot (.) operator to access object's instance variables and assign values
myDog.breed = "Labrador";
myDog.age = 3;
myDog.color = "Brown";
// 3. Call methods
// Use the dot (.) operator to call object's methods
System.out.println("--- My First Dog ---");
myDog.displayInfo(); // Output dog's information
myDog.bark(); // Execute bark behavior
// We can create another independent object
Dog anotherDog = new Dog();
anotherDog.breed = "Husky";
anotherDog.age = 2;
anotherDog.color = "Black and White";
System.out.println("\n--- My Second Dog ---");
anotherDog.displayInfo();
anotherDog.wagTail();
}
}Key Steps Explained
Declaration:
Dog myDog;- This line declares a variable named
myDogof typeDog. At this point, it doesn't point to any specific object yet, and its value isnull.
- This line declares a variable named
Instantiation:
new Dog();newis a Java keyword used to create a new object.Dog()is a call to theDogclass's constructor. A constructor is a special method used to initialize newly created objects. If we don't explicitly define a constructor, Java provides a default no-argument constructor.
Initialization:
myDog = new Dog();- This line assigns the memory address of the newly created
Dogobject to themyDogvariable. Now,myDogrefers to this specificDoginstance.
- This line assigns the memory address of the newly created
Summary
- A class is a blueprint for creating objects, defining attributes and behaviors.
- An object is a concrete instance of a class, with independent attribute values.
- Use the
newkeyword to create objects. - Use the dot (
.) operator to access object attributes and call its methods.
Understanding classes and objects is the cornerstone of learning Java. Almost all Java programs are built by defining classes and creating and manipulating objects of those classes.