Introduction to Classes
Object-oriented programming is the most popular, yet generally least understood programming technology to come about in a while, and it all revolves around the concept of an object. You may have been wondering what the big deal is with objects and object-oriented technology. Is it something you should be concerned with, and if so, why? If you sift through the hype surrounding the whole object-oriented issue, you’ll find a very powerful technology that provides a lot of benefits to software design. The problem is that object-oriented concepts can be difficult to grasp. And you can’t embrace the benefits of object-oriented design if you don’t completely understand what they are. Because of this, a complete understanding of the theory behind object-oriented programming is usually developed over time through practice.
Aristotle was probably the first to begin a careful study of the concept of type; he spoke of “the class of fishes and the class of birds.” The idea that all objects, while being unique, are also part of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword class that introduces a new type into a program.
Simula, as its name implies, was created for developing simulations such as the classic “bank teller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, and units of money—a lot of “objects.” Objects that are identical except for their state during a program’s execution are grouped together into “classes of objects” and that’s where the keyword class came from. Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called objects or instances in object-oriented parlance) and manipulate those variables (called sending messages or requests; you send a message and the object figures out what to do with it). The members (elements) of each class share some commonality: every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state, each account has a different balance, each teller has a name. Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors.
So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.
A class is an object-oriented concept which is used to describe the properties and behavior of a real world entity.
A class implementation consists of following 3 types of information:
- Data Members: The data members of the class are used to store characteristics (properties) of an entity.
- Constructors: The constructors are used to initialize data members (properties) of the entity.
- Methods: The methods represents behavior of the entity.
In object-oriented programming, a class is a construct that is used as a blueprint to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains that instance can be considered as the type of that object, e.g. a type of an object of the "Fruit" class would be "Fruit".
A class usually represents a noun, such as a person, place or (possibly quite abstract) thing - it is a model of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of the concept it represents. It encapsulates state through data placeholders called attributes (or member variables or instance variables); it encapsulates behavior through reusable sections of code called methods.
A class is a user defined data type. The class can be used to hide the implementation details of the program from its user. User can only use data members and methods which are exposed by the class itself. Classes are reusable. You can create a new class by composing different classes.
A Java program may be thought of as a system of interacting objects in which each object has its own state and behavior. There is nothing outside of class in Java. Everything is wrapped in classes in a Java program. You can say that Java programs are collection of classes or written by classes.
Syntax
class MyClass {
// field, constructor, and method declarations
}
Example
1. /**
2. * @(#)Student.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/12
7. */
8.
9. public class Student {
10.
11. int rollNo;
12. String fullName;
13. String address;
14. String gender;
15. String program;
16.
17. /**
18. * Creates a new instance of Student.
19. */
20. public Student() {
21.
22. rollNo = 0;
23. fullName = "";
24. address = "";
25. gender = "";
26. program = "";
27.
28. }
29.
30. public Student(int rollNo, String fullName, String address, String gender, String program) {
31.
32. this.rollNo = rollNo;
33. this.fullName = fullName;
34. this.address = address;
35. this.gender = gender;
36. this.program = program;
37.
38. }
39.
40. public void setRollNo(int rollNo) {
41. this.rollNo = rollNo;
42. }
43.
44. public void setFullName(String fullName) {
45. this.fullName = fullName;
46. }
47.
48. public void setAddress(String address) {
49. this.address = address;
50. }
51.
52. public void setGender(String gender) {
53. this.gender = gender;
54. }
55.
56. public void setProgram(String program) {
57. this.program = program;
58. }
59.
60. public int getRollNo() {
61. return rollNo;
62. }
63.
64. public String getFullName() {
65. return fullName;
66. }
67.
68. public String getAddress() {
69. return address;
70. }
71.
72. public String getGender() {
73. return gender;
74. }
75.
76. public String getProgram() {
77. return program;
78. }
79.
80.
81. /**
82. * @param args the command line arguments
83. */
84. public static void main(String[] args) {
85. // TODO code application logic here
86.
87. //Create an object of Student class by empty constructor.
88. Student qurban = new Student();
89.
90. qurban.setRollNo(123);
91. qurban.setFullName("Qurban Ali");
92. qurban.setAddress("London, UK");
93. qurban.setGender("Male");
94. qurban.setProgram("BCS");
95.
96. System.out.println("First student information is as follows:");
97. System.out.println("Roll No: " + qurban.getRollNo());
98. System.out.println("Full Name: " + qurban.getFullName());
99. System.out.println("Address: " + qurban.getAddress());
100. System.out.println("Gender: " + qurban.getGender());
101. System.out.println("Program: " + qurban.getProgram());
102.
103.
104. Student iram = new Student();
105.
106. iram.setRollNo(124);
107. iram.setFullName("Iram Ali");
108. iram.setAddress("Multan, Pakistan");
109. iram.setGender("Female");
110. iram.setProgram("MCS");
111.
112. System.out.println("nSecond student information is as follows:");
113. System.out.println("Roll No: " + iram.getRollNo());
114. System.out.println("Full Name: " + iram.getFullName());
115. System.out.println("Address: " + iram.getAddress());
116. System.out.println("Gender: " + iram.getGender());
117. System.out.println("Program: " + iram.getProgram());
118.
119. //Create an object of Student class by its constructor having parameters
120. Student jawaad = new Student(125,"Jawaad Ahmed","Lahore, Pakistan","Male","MCS");
121.
122. System.out.println("nThird student information is as follows:");
123. System.out.println("Roll No: " + jawaad.getRollNo());
124. System.out.println("Full Name: " + jawaad.getFullName());
125. System.out.println("Address: " + jawaad.getAddress());
126. System.out.println("Gender: " + jawaad.getGender());
127. System.out.println("Program: " + jawaad.getProgram());
128.
129.
130. }
131.
132. }
Output
First student information is as follows:
Roll No: 123
Full Name: Qurban Ali
Address: London, UK
Gender: Male
Program: BCS
Second student information is as follows:
Roll No: 124
Full Name: Iram Ali
Address: Multan, Pakistan
Gender: Female
Program: MCS
Third student information is as follows:
Roll No: 125
Full Name: Jawaad Ahmed
Address: Lahore, Pakistan
Gender: Male
Program: MCS
In above program, we have created a user defined data type Student class. The Student class is a template which can be used to create objects of students. Every object represents an individual student, and contains its properties and behavior of a student.
On line no 9, we have specified class name as Student. From line 11 to 15, we have listed properties or data members (rollNo, fullName, address, gender, program) of the Student class. The data members are used to store information of a student.
On line no 20, we have created an empty constructor Student() to initialize data members. Constructor and class name is same. The constructor(s) are used to initialize data members of the class at the time of object initialization.
On line no 30, we have created another constructor Student(parameters list) with parameters list. The constructor parameters are used to initialize data members at the time of object initialization.
On line no 22, we have used this keyword. The this keyword is used as reference of current object. Here we have used this keyword to differentiate class data members and constructor variables because both have same names.
On line no 40, we have created a method setRollNo(int rollNo). The method is used to set value of data member (rollNo). Methods are behavior of an object. The methods are used to perform some specific task. They are verbs of a noun (entity) means what a noun (entity) can do.
A method consists of access specifier (public), return type (void or int, float, char, String etc), name of the method, parameter(s) list and block of statements. The method parameters are considered as local variables and are available only within method body.
On line no 88, we have created an object of Student class as qurban. The object qurban is used to hold information of student qurban. In the same way, we have created objects of other students like iram and jawaad.
Classes
Object-oriented programming is the most popular, yet generally least understood programming technology to come about in a while, and it all revolves around the concept of an object. You may have been wondering what the big deal is with objects and object-oriented technology. Is it something you should be concerned with, and if so, why? If you sift through the hype surrounding the whole object-oriented issue, you’ll find a very powerful technology that provides a lot of benefits to software design. The problem is that object-oriented concepts can be difficult to grasp. And you can’t embrace the benefits of object-oriented design if you don’t completely understand what they are. Because of this, a complete understanding of the theory behind object-oriented programming is usually developed over time through practice.
Aristotle was probably the first to begin a careful study of the concept of type; he spoke of “the class of fishes and the class of birds.” The idea that all objects, while being unique, are also part of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword class that introduces a new type into a program.
Simula, as its name implies, was created for developing simulations such as the classic “bank teller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, and units of money—a lot of “objects.” Objects that are identical except for their state during a program’s execution are grouped together into “classes of objects” and that’s where the keyword class came from. Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called objects or instances in object-oriented parlance) and manipulate those variables (called sending messages or requests; you send a message and the object figures out what to do with it). The members (elements) of each class share some commonality: every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state, each account has a different balance, each teller has a name. Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors.
So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.
A class is an object-oriented concept which is used to describe the properties and behavior of a real world entity.
A class is a user defined data type. The class can be used to hide the implementation details of the program from its user. User can only use data members and methods which are exposed by the class itself. Classes are reusable. You can create a new class by composing different classes.
A Java program may be thought of as a system of interacting objects in which each object has its own state and behavior. There is nothing outside of class in Java. Everything is wrapped in classes in a Java program. You can say that Java programs are collection of classes or written by classes.
Syntax
class MyClass {
// field, constructor, and method declarations
}
Example
1. /**
2. * @(#)Student.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/12
7. */
8.
9. public class Student {
10.
11. int rollNo;
12. String fullName;
13. String address;
14. String gender;
15. String program;
16.
17. /**
18. * Creates a new instance of Student.
19. */
20. public Student() {
21.
22. rollNo = 0;
23. fullName = "";
24. address = "";
25. gender = "";
26. program = "";
27.
28. }
29.
30. public Student(int rollNo, String fullName, String address, String gender, String program) {
31.
32. this.rollNo = rollNo;
33. this.fullName = fullName;
34. this.address = address;
35. this.gender = gender;
36. this.program = program;
37.
38. }
39.
40. public void setRollNo(int rollNo) {
41. this.rollNo = rollNo;
42. }
43.
44. public void setFullName(String fullName) {
45. this.fullName = fullName;
46. }
47.
48. public void setAddress(String address) {
49. this.address = address;
50. }
51.
52. public void setGender(String gender) {
53. this.gender = gender;
54. }
55.
56. public void setProgram(String program) {
57. this.program = program;
58. }
59.
60. public int getRollNo() {
61. return rollNo;
62. }
63.
64. public String getFullName() {
65. return fullName;
66. }
67.
68. public String getAddress() {
69. return address;
70. }
71.
72. public String getGender() {
73. return gender;
74. }
75.
76. public String getProgram() {
77. return program;
78. }
79.
80.
81. /**
82. * @param args the command line arguments
83. */
84. public static void main(String[] args) {
85. // TODO code application logic here
86.
87. //Create an object of Student class by empty constructor.
88. Student qurban = new Student();
89.
90. qurban.setRollNo(123);
91. qurban.setFullName("Qurban Ali");
92. qurban.setAddress("London, UK");
93. qurban.setGender("Male");
94. qurban.setProgram("BCS");
95.
96. System.out.println("First student information is as follows:");
97. System.out.println("Roll No: " + qurban.getRollNo());
98. System.out.println("Full Name: " + qurban.getFullName());
99. System.out.println("Address: " + qurban.getAddress());
100. System.out.println("Gender: " + qurban.getGender());
101. System.out.println("Program: " + qurban.getProgram());
102.
103.
104. Student iram = new Student();
105.
106. iram.setRollNo(124);
107. iram.setFullName("Iram Ali");
108. iram.setAddress("Multan, Pakistan");
109. iram.setGender("Female");
110. iram.setProgram("MCS");
111.
112. System.out.println("nSecond student information is as follows:");
113. System.out.println("Roll No: " + iram.getRollNo());
114. System.out.println("Full Name: " + iram.getFullName());
115. System.out.println("Address: " + iram.getAddress());
116. System.out.println("Gender: " + iram.getGender());
117. System.out.println("Program: " + iram.getProgram());
118.
119. //Create an object of Student class by its constructor having parameters
120. Student jawaad = new Student(125,"Jawaad Ahmed","Lahore, Pakistan","Male","MCS");
121.
122. System.out.println("nThird student information is as follows:");
123. System.out.println("Roll No: " + jawaad.getRollNo());
124. System.out.println("Full Name: " + jawaad.getFullName());
125. System.out.println("Address: " + jawaad.getAddress());
126. System.out.println("Gender: " + jawaad.getGender());
127. System.out.println("Program: " + jawaad.getProgram());
128.
129.
130. }
131.
132. }
Output
First student information is as follows:
Roll No: 123
Full Name: Qurban Ali
Address: London, UK
Gender: Male
Program: BCS
Second student information is as follows:
Roll No: 124
Full Name: Iram Ali
Address: Multan, Pakistan
Gender: Female
Program: MCS
Third student information is as follows:
Roll No: 125
Full Name: Jawaad Ahmed
Address: Lahore, Pakistan
Gender: Male
Program: MCS
In above program, we have created a user defined data type Student class. The Student class is a template to create student objects. Every object represents an individual student, and contains its properties and behavior of a student.
On line no 9, we have specified class name as Student. From line 11 to 15, we have listed properties or data members (rollNo, fullName, address, gender, program) of the Student class. The data members are used to store information of a student.
On line no 20, we have created an empty constructor Student() to initialize data members. The constructor(s) are used to initialize data members of the class at the time of object initialization.
On line no 30, we have created another constructor Student(parameters list) with parameters list. The constructor parameters are used to initialize data members at the time of object initialization.
On line no 22, we have used this keyword. The this keyword is used as reference of current object. Here we have used this keyword to distinguish class data members and constructor variables because both have same names.
On line no 40, we have created a method setRollNo(int rollNo). The method is used to set value of data member (rollNo). Methods are behavior of an object. The methods are used to perform some specific task. They are verbs of a noun (entity) means what a noun (entity) can do.
A method consists of access specifier (public), return type (void or int, float, char, String etc), name of the method, parameter(s) list and block of statements. The method parameters are considered as local variables and are available only within method body.
On line no 88, we have created an object of Student class as qurban. The object qurban is used to hold information of student qurban. In the same way, we have created objects of other students like iram and jawaad.
