Class Xii

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Class:- Class is a user defined data types.

Object:- Once the class has been defined we create the variables of that
class using declaration this variable is instance or object of the class.

How to define a Class


class classname
{
field declaration;
method declaration;
}
Every inside the square brackets is optional .this means that the following would be a valid
class definiation.
class empty
{

}
Fields Declaration
Class Rectangle
{
int length;
int width;
}
These variables are only declared and therefore not staroge space has been created in the
memory.
Method declaration
type mehodname (parameter list)
{
method body
}

(int m, flat x, float y) // passing three parameters


( ) Empty list

Example:
Class Rectangle
{
int length, width;
void getdata(int x ,int y)
{
length=x;
width=y;
}
int rectArea() // declaration of another method
{
int area=length *width;
return (area);
}
}
Wrong way to declare a method
void getdata(int x, y) // Incorrect

Scope of Variables
Class Access
{
int x;
void method1( )
{
int y;
y=10; //legal
y=x; // legal
}
void method 2( )
{
int z;
x=5; // legal
z=10; // legal
y=1; //illegal
}
}
Creating Objects: Object in java are created using the new operator.
Rectangle rect1; // declare the object
rect1=new Rectangle( ); // instantiate the object
Both statement can be combined into one as below
Rectangle rect1= new Rectangle ( );
Rectangle rect2= new Rectangle ();

Accessing Class Members:


objectname.variablename=value;
objectname.methodname(parameter list);
Example:
Accessing data members.
rect1.length=15;
rect1.width=10;
rect2.length=20;
rect2.width=12;

Accessing method of a class.

Rect1.getdata(55, 12);
Program:1
class Rectangle
{
int length ,width;
void getdata(int x , int y)
{
length =x;
width = y;
}
int rectArea( )
{
int area;
area =length * width;
return (area);
}
}
Class RectArea
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect1=new Rectangle() ; // creating objects
Rectangle rect2=new Rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1*width;
rect2.getdata(20,12); //Accessing method
area2=rect2.rectArea();
System.out.println(“Area 1=“+ area1);
System.out.println(“Area2=“+ area2);
}
}
Constructor:- There are two way to initialize all the variable of class.
First Way:-
rect1.length=15;
rect1.width=10;
Second Way:-
rect1.getdata(20,12);
Another Way:- Java supports a special type of method ,called
constructor ,that enables an object to initialize itself when it is created.
Constructor have the same name as the class itself ,Secondly they do not
specify a return type ,not even void .
Example:-
class Rectangle
{
int length;
int width;
Rectangle(int x, int y) // constructor method
{
length=x;
width=y;
}
int rectArea( )
{
return (length*width);
}
}
Class RectangleArea
{

public static void main(String args[])

Rectangle rect1=new Rectangle(15,10) // calling constructor

int area1=rect1.rectArea();

System.out.println(“Area1=“+ area1+);

Out put of the programm will be


Area1=150
Types of Constructor
1.Default Constructor.
2.Parameterized Constructor

Default Constructor---A “default constructor” is a constructor with no


parameters.

Parameterized Constructor---A “parameterized constructor” is a


constructor with parameters. The way of declaring and supplying
parameters is same as that with methods.
class cons_param
{
int n,n2;
int sum,sub,mul,div;
public cons_param(int x,int y)
{
n=x;
n2=y;
}
public void opr()
{
sum=n+n2;
sub=n-n2;
mul=n*n2;
div=n/n2;
System.out.printn("Sum is "+sum);
System.out.printn("Difference is "+sub);
System.out.printn("Product is "+mul);
System.out.printn("Quotient is "+div);
}
}
class apply11
{
public static void main(String args[])
{
Cons_param obj=new cons_param(6,17);
obj.opr();
}
}
We can pass parameter in method by two ways -
1.Pass by Value
2.Pass by Address or Reference
Pass by Value-When we pass a data type(like a variable “num” of int, float or any other
type of data) to a method or some constant values like(15,10). They are all passed by
value. A copy of variable’s value is passed to the receiving method and hence any
changes made to the values do not affect the actual variables.
Example---
class pass_by_val
{
int n,n2;
public void get(int x,int y)
{
x=x*x; //Changing the values of passed arguments
y=y*y; //Changing the values of passed arguments
}
}
class apply6
{
public static void main(String args[])
{
int a,b;
a=1;
b=2;
System.out.println("Initial Values of a & b "+a+" "+b);
pass_by_val obj=new pass_by_val();
obj.get(a,b);
System.out.println("Final Values "+a+" "+b);
}
}
Pass by Reference
Objects are always passed by reference. When we pass a value by reference, the reference
or the memory address of the variables is passed.
Demonstrating Pass by Reference---
class pass_by_ref
{
int n,n2;
public void get(int a,int b)
{
n=a;
n2=b;
}
public void doubleit(pass_by_ref temp)
{
temp.n=temp.n*2;
temp.n2=temp.n2*2;
}
}
class apply7
{
public static void main(String args[])
{
int x=5,y=10;
pass_by_ref obj=new pass_by_ref();
obj.get(x,y); //Pass by Value
System.out.println("Initial Values are-- ");
System.out.println(+obj.n);
System.out.println(+obj.n2);
obj.doubleit(obj); //Pass by Reference
System.out.println("Final Values are");
System.out.println(+obj.n);
System.out.println(+obj.n2);
}
class apply7
{
public static void main(String args[])
{
int x=5,y=10;
pass_by_ref obj=new pass_by_ref();
obj.get(x,y); //Pass by Value
System.out.println("Initial Values are-- ");
System.out.println(+obj.n);
System.out.println(+obj.n2);
obj.doubleit(obj); //Pass by Reference
System.out.println("Final Values are");
System.out.println(+obj.n);
System.out.println(+obj.n2);
}
Use of “this” keyword
The “this” keyword refers to the object by which a method was called. Have a look at this
piece of class.
// This is a wrong code…
class no_this
{
int a,b;
void take(int a,int b)
{
a=a;
b=b;
}
The mistake in this program can be easily pointed out. We cannot use the same name for
the instance variables and the variables which are declared as parameters. The reason
being the instance variables are hidden when we use parameters with the same name. If
we are determined to use the same name, then the “this” keyword comes to rescue. Now
look at a class named “with_this”
class with_this
{
int n, n2;
Int sm;
public void get(int n,int n2)
{
this.n=n;
this.n2=n2;
}
public void sum()
{
sm=n+n2;
System.out.println("Sum is "+sm);
}
}
class apply9
{
public static void main(String args[])
{
with_this obj=new with_this();
obj.get(10,12);
obj.sum();
}
}
We can use the “this” keyword even if we have used different names for the
parameters
and the instance variable.
Types of members in a Class:

1.Instance members(Object Members)


2. Static members(Class members)

Instance Members:- The instance methods are those that are defined without the
keyword static .the dot ‘.’ notation with a object reference is used to access instance
methods.

Static Members: The static members are those that are declared with keyword
static.Static members can be accessed either by using the class name or by using the
object reference ,but instance members can only be accessed via object references.
Example:
Class Mathoperation
{
static float mul(float x,float y)
{
return (x*y);
}
public float divide(float x ,float y)
{
return (x*y);
}
}
class MathApplication
{
public void static main(String args[] )
{
float a=Mathoperation.mul(4.0 ,5.0 );
Mathoperation obj1=new Mathoperation();
float b=obj1.divide(6.0 , 2.0);
System.Out.println(“a =“+ a);
System.Out.Println(“b =“ +b);
}
}

You might also like