Tuesday, March 22, 2011

Classes and Objects in Java

class: a set of same type of objects
a blueprint

ex: class Family
class Drama
class Student
class Button
class Connection
class HttpServlet

objects: instances of a class
attributes/variables : properties of those objects
methods: functions/ activities
static variable: common attribute to all objects

ex1)
public class Family {
float height;
String name;
static String surname ="perera";


public static void main(String[] args) {
Family father = new Family();
father.height = 6.0f;
father.name = "sunil";
System.out.println(father.height);
System.out.println(father.name+" "+surname);
System.out.println(surname);
father.sing();

Family son = new Family();
son.name="gayan";
System.out.println(son.surname);
System.out.println("full name: "+son.name+" "+surname);
}

public void sing(){
System.out.println("hehe");
}

}

class: Family
objects:father, son
variables/attributes: name, height
static variable: surname
method:sing


output
sunil perera
perera
hehe
perera
full name: gayan perera

No comments:

Post a Comment