Last Updated: 01 May, 2023
Java is an object-oriented programming language, and for any object-oriented programming language, classes and objects are basic concepts that revolve around real-world entities.
A class is a user-defined data type, prototype, or template in Java from which objects are created. It represents the set of properties, methods etc. that are common to all objects of one type. A single class can have one or more objects.
A class is a logical entity that does not occupy any memory. Memory is only allocated if we create the object of a class.
To create a class, use the keyword class.
In Java, a class can contain the following items:
Java Class Syntax:
The object is an instance of a class. The object is a real-world entity that has states and behaviours. In Java, we can create the object using a new keyword.
An object has the following characteristics:
Object creation Syntax:
For example, Assume Student is a class name and we are creating an object of the Student class.
Example: Java class and Object creation
Output
Product Information
Product class - Default Constructor
Product Name: Pen
Price: 20.5
There are the following five ways to create an object in Java:
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between a class and an object in Java?
In Java, a class is a blueprint or template for creating objects, which define a set of attributes (data fields) and methods (behaviours) that are common to all instances of the class. On the other hand, an object is an instance of a class that is created using the class blueprint. A class defines the properties and behaviours of an object, while an object is an instance of a class that has its own unique state and behaviour. In other words, a class provides a definition for an object, whereas an object is a concrete instance of that definition.
For example, consider a class called "Person". This class could have data fields such as "name", "age", "gender", etc., as well as methods like "getAge()", "setAge()", "getName()", etc. An object of the "Person" class could then be created with specific values for these data fields. For instance, an object of the "Person" class could be created with the name "John", age "30", and gender "male".
What is a variable in Java?
A variable is a data container that contains the data while the Java program is executed. A variable is assigned a single data type, such as String, Integer, Boolean, Float, etc. The data type defines the nature of the data value. A variable is the name of a memory location.
The variable name is a combination of two words: "vary" and "able," which means its value can be changed. Multiple times, different values can be assigned to the same variable.
All Java variables must be identified with unique names. These unique names are called "identifiers.
What is a method in Java?
Ans. A method is a set of instructions. These instructions are enclosed within a pair of curly braces and designed to perform a specific task. Every method has a name, and it may take some parameters and return some values after execution.
In Java, all the methods must be declared inside a class and are executed only when they are called. A method can be called one or more times based on the requirement. A method can be accessed outside the class based on the access modifier. A method is also known as a function.
What is a constructor in Java?
A constructor is like a special method in a Java class; its name is the same as the class name, and it does not return any value after execution.
A constructor is called at the time of object initialization. The JVM first allocates memory for the object and then executes the constructor to initialize the instance variables. Once object creation is completed, constructor execution is also completed.
In Java, every class must have a constructor. If we don’t define a constructor in a class, then the compiler automatically creates a default constructor (with no arguments) for the class.
What is a static variable in Java?
A static variable is a variable that is shared by all objects in a class. It is initialized only once, when the class is loaded, and retains its value throughout the lifetime of the program.
What is a final variable in Java?
A final variable is a variable that cannot be changed once it has been initialized. It is often used to define constants in a program.
What is the default parent class in Java?
The Object class is the default parent class in Java, which is available in the java.lang package. In Java, every class implicitly extends the Object class, even if it does not explicitly specify an extends clause. All subclasses inherit a number of essential methods from the Object class, such as equals(), hashCode(), and toString().
Why is the Object class the default superclass of all the Java classes?
An Object class is the default superclass of all the Java classes because the Object class contains the basic functionality methods such as hashCode(), clone(), toString(), wait(), notify(), etc. that are common to every Java class and are implicitly available in all the Java classes. Users can override these methods if required; otherwise, they can use the default implementation.