unit 2 java
unit 2 java
Class is the blueprint of an object. It is used to create objects. An object is an instance of the class.
No memory is allocated when a class is declared. Memory is allocated as soon as an object is created.
A class is a group of similar objects. An object is a real-world entity such as a book, car, etc.
A class can only be declared once. Objects can be created many times as per requirement.
An example of class can be a car. Objects of the class car can be BMW, Mercedes, Ferrari, etc.
Java Classes
A class in Java is a set of objects which shares common characteristics and common
properties. It is a user-defined blueprint or prototype from which objects are created. For
example, Student is a class while a particular student named Ravi is an object.
int id=6;
String name=“java”;
Output
6 java
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and represents real-life
entities. Objects are the instances of a class that are created to use the attributes and
methods of a class. A typical Java program creates many objects, which as you know, interact
by invoking methods. An object consists of:
•State: It is represented by attributes of an object. It also reflects the properties of an object.
•Behavior: It is represented by the methods of an object. It also reflects the response of an
object with other objects.
•Identity: It gives a unique name to an object and enables one object to interact with other
objects.
Example of an object: Dog
Objects correspond to things found in the real world. For example, a graphics program may have
objects such as “circle”, “square”, and “menu”. An online shopping system might have objects
such as “shopping cart”, “customer”, and “product”.
Dog tuffy;
If we declare a reference variable(tuffy) like this, its value will be undetermined(null) until an
object is actually created and assigned to it. Simply declaring a reference variable does not
create an object.