0% found this document useful (0 votes)
5 views

unit 2 java

The document explains the fundamental concepts of classes and objects in Java, which are essential to Object-Oriented Programming (OOP). It outlines the differences between classes and objects, describes the structure and components of Java classes, and details how to declare and initialize objects. Additionally, it emphasizes that classes serve as blueprints for creating objects, which represent real-world entities with unique states and behaviors.

Uploaded by

csamunder
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

unit 2 java

The document explains the fundamental concepts of classes and objects in Java, which are essential to Object-Oriented Programming (OOP). It outlines the differences between classes and objects, describes the structure and components of Java classes, and details how to declare and initialize objects. Additionally, it emphasizes that classes serve as blueprints for creating objects, which represent real-world entities with unique states and behaviors.

Uploaded by

csamunder
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Classes and Objects in Java

In Java, classes and objects are basic concepts of


Object Oriented Programming (OOPs) that are used to represent real-world concepts
and entities. The class represents a group of objects having similar properties and
behavior. For example, the animal type Dog is a class while a particular dog
named Tommy is an object of the Dog class. In this article, we will discuss Java
classes and objects and how to implement them in our program.

Difference Between Java Classes and Objects


The main differences between class and object in Java are as follows:
Class Object

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.

Class is a logical entity. An object is a physical entity.

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.

Properties of Java Classes


•Class is not a real-world entity. It is just a template or blueprint or prototype from which
objects are created.
•Class does not occupy memory.
•Class is a group of variables of different data types and a group of methods.
•A Class in Java can contain:
• Data member
• Method
• Constructor
• Nested Class
• Interface
Class Declaration in Java
access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}
Components of Java Classes
In general, class declarations can include these components, in order:
•Modifiers: A class can be public or has default access (Refer this for details).
•Class keyword: Class keyword is used to create a class.
•Class name: The name should begin with an initial letter (capitalized by convention).
•Superclass (if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass
one parent.
•Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can
implement more than one interface.
•Body: The class body is surrounded by braces, { }.
// Java Class example
class Student {

int id=6;

String name=“java”;

public static void main(String args[])


{
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

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”.

Declaring Objects (Also called instantiating a Class)


When an object of a class is created, the class is said to be instantiated. All the instances
share the attributes and the behavior of the class. But the values of those attributes, i.e. the
state are unique for each object. A single class may have any number of instances.
As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With
a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variables , the type
must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.

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.

Initializing a Java Object


The new operator instantiates a class by allocating memory for a new object and returning a
reference to that memory. The new operator also invokes the class constructor.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy