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

Class Fundamentals

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 views17 pages

Class Fundamentals

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/ 17

Class fundamentals

• What is a class?
• A class is a template or blueprint from which objects are
created. (OR)
• A class is a collection of data members and member
functions.
• What are data members?
• Data members are nothing but simply variables that we
declare inside the class, so it called data member of that
particular class
• What are member functions?
• Member functions are the functions or methods which
we declare inside the class , so it is called member
function of that particular class
• General form / syntax of a class:
class classname
{
type instance-variable; //data members

type methodname(parameter-list) // member function


{
body of method
}
}
• A class is declared by using class keyword
• A class can contain any of the following variable types.
• Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.
• Instance variables − Instance variables are variables
within a class but outside any method. These variables
are initialized when the class is instantiated(or when an
object is created).
• Example of a class
class student
{
String name;
void display()
{
name="sreeRam";
System.out.println("the name is : "+name);
}
}
• Declaring objects:
• Object is an instance of a class. Class is a template or
blueprint from which objects are created. So object is the
instance(result) of a class.
• Object Definitions:
• Object is a real world entity.
• Object is a run time entity.
• Object is an entity which has state and behavior.
• Object is an instance of a class.

• In Java, the new keyword is used to create objects


• Syntax:
• classname objectname = new classname( );
• Example of a class and object
class student
{
String name;
void display()
{
name="sreeRam";
System.out.println("the name is : "+name);
}
}
class studentDemo
{
String name;
public static void main(String args[])
{
student st = new student();
st.display();

}
• Methods:
• Java programmers use methods to describe behaviors. A
method declaration has the following syntax:
type methodname(parameter-list)
{
// method body
}

• The type specifies the type of data returned by the


method.
• If the method does not return a value, its return type
must be void.
• The name of the method is specified by methodname
• Parameters list is optional, i.e.,
• If the method has no parameters, then the parameter list
will be empty.
class rectangle // example for method
{ int length;
int breadth;
int area()
{
return length * breadth;
}
} // ouput
class rectangleDemo the area is
200
{ public static void main(String args[])
{ int res;
rectangle obj= new rectangle();
obj.length = 10;
obj.breadth = 20;
res = obj.area();
System.out.println("the area is : "+res);
Method that takes parameters
class rectangle
{ int area(int length , int breadth)
{
return length * breadth;
}
} // ouput
class rectangleDemo the area is 200
{ public static void main(String args[]) the area is 600
{ int res;
rectangle obj= new rectangle();

res = obj.area(10 , 20);


System.out.println("the area is : "+res);

res = obj.area(20 , 30);


System.out.println("the area is : "+res);
} }
Constructors
• A constructor is a special member function whose main
operation is to allocate the required resources such as
memory and initialize the objects of its class.
• A constructor is distinct (special) from other functions of
the class because their name is same as the class name
they belong to.
• When an object is created constructor is executed
automatically.
• It is called constructor because it constructs the values of
data members of the class.
• Constructor of a class is the first member function to be
executed automatically when an object of the class is
created.
• Constructor can be of two types :
1) Default constructors 2) Parameterized constructors
• default constructors: constructors without parameters are
called default constructors.
class Rectangle // eg: for default constructors:
{ int length;
int width;
Rectangle () // Constructor method
{ length = 10;
width = 5;
}
int rectArea()
{
return(length * width)
} }
class RectangleDemo
{
public static void main (string args[ ])
{
Rectangle rect = new Rectangle();
int area = rect. rectArea( );
System.out.println(“Area = ”+ area);
}
}

//output
Area = 50
• Parameterized constructors: constructors with parameters
are called parameterized constructors.
class Rectangle // eg: for parameterized constructors:
{ int length;
int width;
Rectangle ( int x , int y) // Constructor method
{ length = x;
width = y;
}
int rectArea()
{
return(length * width)
} }
class RectangleDemo
{
public static void main (string args[ ])
{
Rectangle rect = new Rectangle(5 , 10);
int area = rect. rectArea( );
System.out.println(“Area = ”+ area);
}
}

You might also like