Skip to content

Q 27 - Classes and Objects #31

Discussion options

You must be logged in to vote

🧱 Classes and Objects in Java

Java is an object-oriented programming language, and its core building blocks are classes and objects.


🏗️ What is a Class?

A class is a blueprint or template for creating objects. It defines:

  • Fields (variables)
  • Methods (functions)
  • Constructors
  • Access modifiers

✅ Example:

class Car {
    String color;
    int speed;
void drive() {
    System.out.println("Car is driving");
}

}


🧍 What is an Object?

An object is an instance of a class. It represents a real-world entity with state and behavior.

✅ Example:

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car(); // Object creation
        myCar.color = "Red";
        m…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by HARSHITH-MV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants