DAY 3

DAY 3
JAVA
CLASS:
class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. It is collection of variables and methods. 
class class_name
{
data_type variable_name 1=value;   // instance variables
.......
data_type variable_name n=value;
return_type metod_name(parameters);
{
int i;    //local variables
.......
}
Class with Variables  
object: An object is an instance of a class. Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
class a
{
int l;
int b;
int h;
}

class demo
{
public static void main(String s[])
{
a obj= new a();
obj.l=10;
obj.b=20;
obj.h=30;
System.out.println(obj.l);

a obj1; // reference object
obj1=obj;
obj1.l=20;
System.out.println(obj1.l);
}
}
output:
10
20

class with method
class a
{
int l;
int b;
int h;
void volume()
{
System.out.println(l*b*h);
}

class demo
{
public static void main(String s[])
{
a obj= new a();
obj.l=10;
obj.b=20;
obj.h=30;
obj.volume();
}
}
output:
6000

Method with parameters: 
class a
{
int l;
int b;
int h;
void setdem(int x, int y, int z)
{
l=x;
b=y;
h=z;
}
void volume()
{
System.out.println(l*b*h);
}

class demo
{
public static void main(String s[])
{
a obj=new a();
obj.setdem(10,20,30);
obj.volume();
}
}

Method with RETURN TYPE:
class a
{
int l;
int b;
int h;
int volume()
{
int n=l*b*h;
return n;
}

class demo
{
public static void main(String s[])
{
a obj= new a();
obj.l=10;
obj.b=20;
obj.h=30;
int v= obj.volume();
System.out.println(v);
}
}
output: 
6000

METHOD OVERLOADING
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
In order to overload a method, the argument lists of the methods must differ in either of these:
1. Number of parameters.

2. Data type of parameters.
3. Sequence of Data type of parameters.

NUMBER OF PARAMETERS
class Adder{  
static int add(int a,int b){return a+b;}  
static int add(int a,int b,int c){return a+b+c;}  
}  
class TestOverloading1{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(11,11,11));  

}
}  
OUTPUT:
22
33

DATATYPE OF PARAMETERS
class Adder{  
static int add(int a, int b){return a+b;}  
static double add(double a, double b){return a+b;}  
}  
class TestOverloading2{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(12.3,12.6));  
}
}
OUTPUT:
22
24.9  

Comments

Popular posts from this blog

Day 7

Industrial training in CDAC