Java Classes and Objects

Java Classes and Objects

Java is an object-oriented programming language, so classes and objects are the foundation of Java programs.


1. What is a Class?

A class is a blueprint or template that defines the attributes (variables) and behaviors (methods) of objects.


 

  • Class name starts with an uppercase letter by convention.

  • Attributes → Store object data.

  • Methods → Define actions the object can perform.


2. What is an Object?

An object is an instance of a class. It has its own copy of the class attributes and can use class methods.


 

📌 Output:

Brand: Toyota, Year: 2022
Brand: Honda, Year: 2023

3. Object Syntax

  • new keyword creates a new object in memory.

  • Each object has its own copy of instance variables.


4. Multiple Objects Example


 

📌 Output:

Name: Alice, Age: 20
Name: Bob, Age: 22

5. Key Points

  • Class → Blueprint (defines properties and behaviors).

  • Object → Instance of a class (has its own state).

  • Attributes → Variables inside a class.

  • Methods → Functions inside a class.

  • new keyword → Creates object in memory.

  • Objects can have different values for the same attributes.

You may also like...