Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we’ll discuss what a class is in Java. Can anyone tell me what a class represents?
Isn't it like a blueprint for creating objects?
Great! Yes, a class acts like a blueprint or template. It defines the properties and behaviors of the objects created from it. Remember the acronym PB for Properties and Behaviors.
What do you mean by properties and behaviors?
Properties are the attributes of the class, while behaviors are what the class can do or the methods it defines. For example, in a Student class, the properties could be id and name, while a behavior could be a method named display.
Can you show us how this looks in code?
"Certainly! Here's a simple example:
Now that we've covered classes, let’s move on to objects. What do we call the real-world entities created based on a class?
Those would be objects, right?
Exactly! An object is an instance of a class with actual values. If we look at our Student class, how can we create an object of that class?
We can use the new keyword, right?
Correct! We create an object using the syntax Student s1 = new Student();. This creates an instance of Student. Let’s say we assign values to id and name, how would we display them?
We would call the display method, like this: s1.display();.
Exactly! And it will print the id and name. Can anyone tell me what the output would be if we set id to 101 and name to 'Aman'?
It would display 'ID: 101' and 'Name: Aman'.
Fantastic! Remember, classes are blueprints, while objects are the real instances with actual data.
A blueprint defining properties and behaviors for creating objects.
An instance of a class representing a real-world entity.
Attributes defined in a class that represent characteristics of an object.
Methods defined in a class that allow an object to perform actions.
In Java's code, a class will show, properties and behaviors in a neat row.
Imagine a blueprint for a house (the class) - without it, you can't visualize the house (the object) you want to build.
Think of 'CPB': Class - Properties - Behaviors to remember how a class structures objects.
C.O.B. - Class Defines Objects Behavior.
Term
What is a class?
Click to flip
Term
What is an object?
Click to flip
Term
What are properties?
Click to flip
Term
What are behaviors?
Click to flip
A blueprint or template for creating objects that defines properties (fields) and behaviors (methods).
A real-world entity based on a class that holds actual values.
Attributes or fields defined in a class that define the characteristics of an object.
Methods or functions defined in a class that describe what operations can be performed by an object.
This section introduces classes and objects in Java, explaining their roles as blueprints and instances, respectively.
In this section, we explore the concepts of classes and objects in Java programming. A class serves as a template for creating objects, defining their properties and behaviors. An object is a specific instance of a class, containing actual values that represent real-world entities. Examples and syntax are provided for further clarification.
In the realm of Object-Oriented Programming (OOP) in Java, the concepts of Classes and Objects are fundamental. A class is defined as a blueprint or template for creating objects. It encapsulates data in the form of properties (often referred to as fields or attributes) and functionalities through behaviors (known as methods or functions).
The syntax for declaring a class in Java is as follows:
class ClassName {
// variables
// methods
}An object, on the other hand, is a concrete instance of a class. It holds actual values corresponding to the properties defined in the class. For instance, consider the following class definition for a Student:
class Student {
int id;
String name;
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
}This class can be instantiated to create objects that store real student information:
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Creating object
s1.id = 101;
s1.name = "Aman";
s1.display(); // Calling method
}
}The output generated would be:
ID: 101
Name: AmanThis demonstrates how classes serve as templates and how objects are utilized to bring the data and methods to life, encapsulating the concept of representation in OOP.