Loading
Java - Object and Class-interview

Q1. What is a class in Java?

A class is a blueprint or template for creating objects. It defines properties (fields) and behaviors (methods) that objects of that class can have.






Q2. What is an object in Java?

An object is an instance of a class. It has a state (values of fields) and behavior (methods) defined by its class.







Q3. What is the difference between a class and an object?

Feature

Class

Object

Definition

Blueprint for objects

Instance of a class

Memory

No memory allocation

Memory allocated at runtime

Behavior

Defines methods

Executes methods

Creation

Declared using class keyword

Created using new keyword







Q4. How do you create an object in Java?

Objects are created using the new keyword:


ClassName obj = new ClassName();


This allocates memory and calls the constructor to initialize the object.








Q5. Can a class exist without an object?

 Yes.

  • A class is a blueprint and exists independently in the code.
  • Objects are created at runtime; a class can exist without creating any object.