Monday, October 15, 2012

What is Object and How can we use that from different class?



Here we need two different class to describe the Object. 
We are taking Human and HumanTest as our class.


______________________
HUMAN
_______________
sex
name
height
____________
talk()


- A Class


class Human{

String sex;                    //    instant variables
String name;            //    instant variables
float height = 6;              //    instant variables

void talk(){      // a method
    System.out.println("Hey, my name is Lanny!!!!");
       }
}

- A tester class which access objects, variables and methods


class HumanTest {

 public static void main(String[] args){

Human h = new Human(); // a human object

h.sex = "male"; // use dot operator (.) to set sex of human and do same with all variables
h.name = "Larry";
h.height = 6.0;

h.talk(); // to call talk method

     }
}


You can add more object in that, e.g Human l = new Human();, Human r = new Human(); etc and call their different methods too.



Thank you for viewing this post.........

No comments:

Post a Comment