0% found this document useful (0 votes)
7 views35 pages

Unit-2 Full

The document provides an overview of classes, objects, and methods in Java, explaining how classes serve as blueprints for creating objects with defined properties and behaviors. It covers the syntax for declaring classes, adding instance variables and methods, creating objects, and using constructors. Additionally, it discusses concepts like method overloading, polymorphism, and static members in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

Unit-2 Full

The document provides an overview of classes, objects, and methods in Java, explaining how classes serve as blueprints for creating objects with defined properties and behaviors. It covers the syntax for declaring classes, adding instance variables and methods, creating objects, and using constructors. Additionally, it discusses concepts like method overloading, polymorphism, and static members in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CLASSES OBJECTS

AND METHODS
Class
A class is a user-defined data type with a template that
serves to define its properties

In Java, a class is a blueprint for creating objects. It


defines the properties (fields or attributes) and
behaviors (methods) that the objects created from
the class will have. Classes are the fundamental
building blocks of object-oriented
programming (OOP) in Java.
SYNTAX
Class rectangle
{
int length;
EXAMPLE
int width;
}
Adding variable
1. Data is encapsulated in a class by placing
data fields inside the body of the class definition.
2. These variables are called instance
variable because they are created whenever an
object of the class is initiated.
3. We can declare the instance variables
exactly the same way as we declare local variables.
Class rectangle
{
int length;
EXAMPLE
int width;
}
Adding methods:
Methods are necessary for manipulating the data
contained in the class.
Methods are declared inside the body of
the class but immediately after the declaration of
instance variables.
SYNTAX
type methodname( parameter - list)
{
method - body;
}
class rectangle
{ int length;
int width;
void getData ( int x, int y)
{
EXAMPLE length=x;
width=y;
}
}
Parts of method declarations :

1. Method name - Name of the method


2. Type - Type of the value the method returns
3. Parameter-list - A list of parameters
4. Body - The body of the method
Creating Objects:
An object in java is essentially a block of
memory that contains space to store all the
instance variables.
Creating an object is also referred to as
instantiating an object.
SYNTAX

Rectangle rectl; //declare


rect1=new Rectangle(); //instantiate
Creating object references
Accessing class members

In Java, accessing class members refers to


the process of using the dot (.) operator to
access the fields (variables) and methods
(functions) of a class or object.
SYNTAX
rect1.length=15;
rect1.width=10;
rect2.length=20;
EXAMPLE rect2.width=12;
This is one way of assigning values to the variables in the objects.
Constructors:
A constructor in Java is a block of code that is
executed when an object of a class is instantiated
(created). Its primary purpose is to initialize the object's
state by assigning values to its fields.
Constructors have the same name as the class
itself.
Constructors do not specify a return type, not
even void . This is because they return the instance
of the class itself
Class Rectangle
{
int length;
int width;
Rectangle(int x,int y)//constructor method
{
length=x;
width=y;
EXAMPLE }
int rectArea()
{
return(length*width);
}
}
Application of Constructor
class Rectangle
{ int length, width ;
Rectangle ( int x, int y)
{ length = x;
width = y;
} Output Area = 150
int rectArea()
}
}
class RectangleArea
{
public static void main (string args[ ])
{ Rectangle rectl = new Rectangle (15,10);
int areal = rectl.rectArea ( );
System.out.println ("Areal="+area1);
}
}
Methods overloading:
In java, it is possible to create methods that
have the same name, but different
parameters lists and different definitions.
This is called method overloading.
Polymorphism:
When we call a method in an object, Java
matches up the method name first and then
the number and type of parameters to decide
which one of the definitions to execute. This
process is known as polymorphism
class Room
{
float length;
float breadth;
Room(float x, float y)
{
length=x;
breadth=y;
}
Room ( float x)
{
EXAMPLE length=breadth=x;
}
int area()
{
return ( length*breadth);
}
}
Static Members:
• A class contains two sections .
• One declares variables and the other declares methods. These
variables and methods are called instance variables and instance
methods.
• In Java, static members are fields (variables) or methods that
belong to a class rather than to any specific instance of the class.
They are shared across all instances of the class, meaning there
is only one copy of the static member, regardless of how many
objects of the class are created.
• Every time the class is instantiated, a new copy of each of them
is created. They are accessed using the objects (with Dot
operator)
• Static Variables are used when we want to have a variable
common to all instances of a class
SYNTAX
EXAMPLE
ARRAY:
One Dimensional Array:
Example:
Creating an Array:
Initialization of Array:
Two Dimensional Array:
String:
Syntax:
Example:

You might also like