Sunday 12 February 2012

what is class

class

is a blue print for construct anything .
and user can crate that things by making the object of that class.
in real world ther are many objects of same kind. like a normal scooter is
an object of two wheeler class. and a bike is also an object of two wheeler class

the

class

has some properties and some methods.
we can use that methods by created an object of that class.

here is an example of class:
public class twowheelers{

int wheels=2;
int gears=4;
int seats;
int petrol;

public void changegear(int gears ){
this.gears=gears;
}
public void setPetrol(int petrol){
this.petrol=petrol;
}
public void changeSeat( int seats){
this.seats=seats;
}
public void showDetail(){
System.out.println("wheels="+wheels+ " gears:"+gears + " seats: "+seats +" petrol:"+petrol+"lt");

}
}

now we are creating an demo class in which we are crating two objects of twowheers class :


public class democlass {


public static void main(String[] args) {
twowheelers scooter=new twowheelers(); // first object
twowheelers bike = new twowheelers(); // second object

scooter.changegear(4);
scooter.changeSeat(2);
scooter.setPetrol(10);
scooter.showDetail();

bike.changegear(5);
bike.changeSeat(2);
bike.setPetrol(12);
bike.showDetail();
}
}


output:


wheels=2 gears:4 seats: 2 petrol:10lt
wheels=2 gears:5 seats: 2 petrol:12lt

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...