Class A class is a user defined prototype or blueprint from which objects are created. We can also say class is collection of data members and member functions/methods. where data members are variable and member functions/methods are used to access data members. Keyword: class
Note: Every Single Java file can have only single/one public class. We can't have two or more class as public within single java file.
Object Objects are instance of class. In other words object reference are memory locations where object data is saved. Object of predefined or user defined classes can be created by using new operator.
Constructor Constructors are special method which has same name as that of class name.
class Student{ String name;//data members int rollno;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } //2 Member Functions public void sleep() { System.out.println(name+" "+rollno + " is Sleeping" ); } public void study() { System.out.println(name+" "+rollno + " is Studing" ); } }
public class DemoClassObject {
public static void main(String[] args) { Student s1;//s1 is object of Student Class s1 = new Student();//Initialize
0 comments:
Post a Comment