Class
-------
A class is the template or
blueprint from which objects are made.They define the structure of the objects.
The general form of a Class
---------------------------------
class class-name{
type instance-variable1;
type instance-variable2;
......
.....
type instance-variableN;
type methodname1(parameter-list)
{
body of method
}
type methodname2(parameter-list)
{
body of method
}
......
.........
type methodnameN(parameter-list)
{
body of method
}
The data, or variables, defined within a class are called
instance variables. The code contained
within methods. Collectively, the methods and variables defined within a class
are called members of the class.
ex:
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);
}
}
Relationship between classes
-----------------------------------
·
Dependence(uses-a)
·
Aggregation(Has-a)
·
Inheritance(is-a)
Objects
-----------
Objects is an instance of
class.
Three key characteristics of
objects
------------------------------------------
·
Object’s
behavior—What can you do with this object,or what methods can you apply to it?
·
Object’s
state---How does the object react when you apply those methods?
·
Object’s
identity---How is the object distinguished from others that may have the same behavior and state?
Objects in java are created
using the new operator. The new operator
creates an object of the specified class and returns a reference to that
object.
ex:
Rectangle rect1;//declare the object
rect1=new Rectangle();//instantiate the object
Both statement can be combined into one :
Rectangle rect1=new Rectangle();
Accessing class Members :
--------------------------------
objectname.variablename=value;
objectname.methodname(parameter-list);
ex:
rect1.length=10;
rect1.width=10
rect1.getData(15,10);
ex:1
class Rectangle
{
int length,width;
void getData(int x,int y)
{
length=x;
width=y;
}
int rectArea()
{
int area=length*width;
return(area);
}
}
class RectArea
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.length=15;
rect1.width=20;
area1=rect1.length*rect1.width;
rect2.getData(10,12);
area2=rect2.rectArea();
System.out.println("Area1="+area1);
System.out.println("Area2="+area2);
}
}
//Returning a Value
-------------------------
class Box
{
double width,height,depth;
double volume(){
return width*height*depth;
}
}
class BoxDemo1
{
public static void main(String args[]){
double vol;
b1.width=10;
b1.height=20;
b1.depth=2;
b2.width=3;
b2.height=4;
b2.depth=5;
vol=b1.volume();
System.out.println("Volume is :"+vol);
vol=b2.volume();
System.out.println("Volume is :"+vol);
}
}
//Parameterized method
---------------------------
class Box
{
double width,height,depth;
double volume()
{
return width*height*depth;
}
void setDim(double w,double h, double d){
width=w;
height=h;
depth=d;
}
}
class BoxDemo2
{
public static void main(String args[]){
double vol;
b1.setDim(10,20,15);
b2.setDim(3,6,9);
vol=b1.volume();
System.out.println("Volume
is :"+vol);
vol=b2.volume();
System.out.println("Volume is :"+vol);
}
}
------------------
import java.io.*;
class Emp
{
String name;
int s;
void set(String n,int s1)
{
name=n;
s=s1;
}
void show(){
System.out.println(name+" "+s);
}
}
class Display{
public static void main(String args[])
{
Emp e1=new Emp();
e1.set("Sita",5600);
e1.show();
}
}
//return type function
------------------------
import java.io.*;
class Emp
{
String name;
double s,hra,da;
void set(String n,double s1)
{
name=n;
s=s1;
}
double show()
{
hra=s*0.25;
da=s*0.71;
return (s+hra+da);
}
}
class Display1{
public static void main(String args[])throws IOException
{
String name1;
int n;
Emp e=new Emp();
DataInputStream din=new
DataInputStream(System.in);
System.out.println("Enter name and salary
:");
name1=din.readLine();
n=Integer.parseInt(din.readLine());
e.set(name1,n);
System.out.println(e.name+" "+e.show());
}
}
Constructor
-------------
A constructor initializes an
object immediately upon creation. It has
the same name as the class. Once
defined, the constructor is automatically called immediately after the object
is created, before the new operator completes.
Constructor have no return type, not even void.
ex:
class product
{
String name;
int price;
product()
{
name="Mobile";
price=5400;
}
void show()
{
System.out.println(name+" "+price);
}
}
class cons
{
public static void main(String arg[])
{
product p1=new product();
p1.show();
}
}
---------------------
ex:
class Box
{
double width,height,depth;
double volume()
{
return width*height*depth;
}
Box()
{
System.out.println("Constructing Box");
width=10;
height=20;
depth=5;
}
}
class BoxDemo3
{
public static void main(String args[])
{
double vol;
vol=b1.volume();
System.out.println("Volume is :"+vol);
vol=b2.volume();
System.out.println("Volume is :"+vol);
}
}
Parameterized Constructor
--------------------------------
class Box
{
double width,height,depth;
double volume(){
return width*height*depth;
}
Box(double w,double h,double d){
width=w;
height=h;
depth=d;
}
}
class BoxDemo4
{
public static void main(String args[]){
double vol;
vol=b1.volume();
System.out.println("Volume is :"+vol);
vol=b2.volume();
System.out.println("Volume is :"+vol);
}
}
------------------------------------------------
class RectangleArea
{
int length,width;
RectangleArea(int x,int y)
{
length=x;
width=y;
}
int rectArea()
{
return (length*width);
}
}
class Rectangle
{
public static void main(String args[])
{
RectangleArea r1=new
RectangleArea(15,10);//Calling constructor
int area1=r1.rectArea();
System.out.println("Area="+area1);
}
}
-------------
Constructor Overloading
------------------------------
class nos
{
int a,b,c;
nos()
{
a=10;
b=20;
c=30;
}
nos(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void show()
{
System.out.println("sum="+(a+b+c));
}
}
class consover
{
public static void main(String arg[])
{
nos n1=new nos();
nos n2=new nos(1,2,3);
n1.show();
n2.show();
}
}
The this keyword
--------------------
this can be used inside any
method to refer to the current object.
class Box
{
double width,height,depth;
double volume()
{
return width*height*depth;
}
Box(double width,double height,double depth)
{
this.width=width;
this.height=height;
this.depth=depth;
}
}
class BoxDemo5
{
public static void main(String args[])
{
double vol;
vol=b1.volume();
System.out.println("Volume is :"+vol);
vol=b2.volume();
System.out.println("Volume is :"+vol);
}
}
The finalize() Method
--------------------------
Used to deallocate the memory
allotted by constructor. It is automatically executed from the garbage
collector.
syntax:
protected void finalize()
{
}
Methods Overloading
--------------------------
It is possible to create
methods that have the same name, but different parameter lists and different
definitions. This is called method
overloading. Method overloading is used
when objects are required to perform similar tasks but using different input
parameters. 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.
//Demonstrate method
overloading.
------------------------------------------
class Overload
{
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a="+a);
}
void test(int a,int b)
{
System.out.println("a and b :"+a+" "+b);
}
double test(double a)
{
System.out.println("double a="+a);
return a*a;
}
}
class OverloadDemo
{
public static void main(String args[])
{
Overload ob=new Overload();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.2);
System.out.println("Result of ob.test(123.2):"+result);
}
}
---------
class shape
{
int length,width;
float radius;
int area(int l,int b)
{
length=l;
width=b;
return (length*width);
}
double area(float r)
{
radius=r;
return (3.14*radius*radius);
}
}
class Overload
{
public static void main(String args[])
{
shape s=new shape();
System.out.println("Area of
rectangle="+s.area(10,20));
System.out.println("Area of
circle="+s.area(2.f));
}
}
Static methods
------------------
Static variables are used
when we want to have a variable common to all instances of a class. Java
creates only one copy for a static variable which can be used even if the class
is never actually instantiated.
static variable and static
methods can be called without using the objects.
ex:
float x=Math.sqrt(25);
Static have several
restrictions:
-------------------------------------
1) They can only call other
static methods.
2) They must only access
static data.
3) They cannot refer to this
or super in any way.
class Mathoperation
{
static float mul(float x,float y)
{
return x*y;
}
static float div(float x,float y)
{
return(x/y);
}
}
class MathApplication
{
public static void main(String args[])
{
float a=Mathoperation.mul(4,5);
float b=Mathoperation.div(a,4);
System.out.println(b);
}
}
---------------
class StaticDemo
{
static int a=42;
static int b=99;
static void callme()
{
System.out.println("a="+a);
}
}
class UseStatic
{
public static void main(String args[])
{
StaticDemo.callme();
System.out.println("b="+StaticDemo.b);
}
}
Nesting of Methods
-----------------------
A method can be called by using only its name by another method of
the same class. This is known as nesting
of methods.
class Nesting
{
int m,n;
Nesting(int x,int y)
{
m=x;
n=y;
}
int largest()
{
if(m>=n)
return m;
else
return n;
}
void display()
{
int large=largest();
System.out.println("Largest
value="+large);
}
}
class NestingTest
{
public static void main(String args[])
{
Nesting nest=new Nesting(10,45);
nest.display();
}
}
No comments:
Post a Comment