SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
Classes and objects
1. Lesson 1 of 4 Object Oriented Programming in Java
1 of 9
Lesson 1: Classes and Objects
Author: Kasun Ranga Wijeweera
Email: krw19870829@gmail.com
Date: 2016 December 12
There are students and teachers in an educational institute. The
institute conducts a course which consists of two subjects:
Mathematics and English. The students have to sit for two
examinations at the end the course. The students are graded based on
their average marks. The salary of a teacher is computed from the
number of teaching hours. Suppose we have to develop an
information system for this institute.
Class: A template that represents the entities with common attributes
and methods.
Therefore we can identify two classes: Student and Teacher.
Attributes of the class Student: name, age, marksMaths,
marksEnglish, average, avg
Attributes of the class Teacher: name, age, hours, rate, salary
Student
name
age
marksMaths
marksEnglish
average
avg
calAverage
getGrade
calAvg
Teacher
name
age
hours
rate
salary
calSalary
2. Lesson 1 of 4 Object Oriented Programming in Java
2 of 9
Methods of the class Student: calAverage, getGrade, calAvg
Methods of the class Teacher: calSalary
Object: Object is an instance of a class.
Following is an example for an object of class Student.
The attributes: name, age, marksMaths, marksEnglish are given as
inputs when creating the object. The attributes: average, avg should
be computed by the system. Here the values of attributes: average,
avg have not yet been computed and they store their default values.
What is the difference between a class and an object? Class does not
have values assigned to the attributes whereas the objects have.
What is a java program? A Collection of classes is called a java
program.
The set of attributes and the set of methods of a class are called state
and behavior of the class respectively.
There are eight primitive data types in java: boolean, byte, char,
short, int, long, float, double.
name = ‘A’
age = 23
marksMaths = 67
marksEnglish = 84
average = 0.0
avg = 0.0
3. Lesson 1 of 4 Object Oriented Programming in Java
3 of 9
Data Type Default Value
boolean false
byte 0
char ‘u0000’
short 0
int 0
long 0L
float 0.0f
double 0.0d
Now let’s try to implement using java programming language. Create
a folder called Codes. Create two java files called Student.java and
Test.java inside that folder. The contents of those files should be as
follows.
Student.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
}
Test.java
class Test
{
public static void main(String args[])
{
}
}
4. Lesson 1 of 4 Object Oriented Programming in Java
4 of 9
We have not yet included everything inside the class Student for the
time being. The class Test contains the main method. We have to
compile and run the class Test only. You have to use Command
Prompt to compile and run. You must be inside the folder Codes in
order to do that.
Example
D:LGOOPNotesCodes>javac Test.java
D:LGOOPNotesCodes>java Test
The first line compiles the code and the second line runs the code.
You have to use command cd to go inside the folder Codes. The
following figure explains this further.
Driver class: The class in which the main method exists.
Here Test.java is our driver class. Every program must have exactly
one driver class. The execution of the program begins inside the body
of the main method.
5. Lesson 1 of 4 Object Oriented Programming in Java
5 of 9
We can create an object from our class Student as follows.
Student s;
s = new Student();
The first line declares a variable of type Student. It is called a
reference variable and created on stack memory. It behaves like
normal variables int x, double y, etc. In the second line, the keyword
new creates an object of type Student and assigns its memory location
to the reference variable s. Note that the objects are always created on
the heap memory.
Then modify your Test.java file as follows and again compile & run.
Test.java
class Test
{
public static void main(String args[])
{
Student s;
s=new Student();
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.marksMaths);
System.out.println(s.marksEnglish);
System.out.println(s.average);
}
}
We can use dot operator (.) to access attributes of an object.
Following figure shows the output in Command Prompt.
6. Lesson 1 of 4 Object Oriented Programming in Java
6 of 9
We did not assign values to the attributes of the object. Therefore java
automatically assigns default values to the attributes.
We can use dot operator to assign values to the attributes of an object
as well. Then Test.java file is as follows.
7. Lesson 1 of 4 Object Oriented Programming in Java
7 of 9
Test.java
class Test
{
public static void main(String args[])
{
Student s;
s=new Student();
s.name='A';
s.age=23;
marksMaths=67;
marksEnglish=84;
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.marksMaths);
System.out.println(s.marksEnglish);
System.out.println(s.average);
}
}
Exercise
1. Create following Student objects. We have already created the
first one.
Object name age marksMaths marksEnglish
s ‘A’ 23 67 64
s1 ‘B’ 34 45 88
s2 ‘C’ 22 29 93
s3 ‘D’ 17 74 38
2. Compute the value of the attribute average for each object.
8. Lesson 1 of 4 Object Oriented Programming in Java
8 of 9
Suppose we execute following line of code.
s = s1
Now the variable s stores the memory location of the object with the
name ‘B’. And the variable s1 also stores the memory location of the
object with the name ‘B’. However no variable stores the memory
location of the object with the name ‘A’. Therefore the object with the
name ‘A’ will be destroyed from the memory.
The objects without references are automatically deleted from the
memory.
The attributes can be divided into two categories.
Instance attribute: An instance attribute is an attribute defined in a
class for which each object of the class has a separate copy.
Class attribute: A class attribute is an attribute defined in a class of
which a single copy exists, regardless of how many objects of the
class exist.
The attributes: name, age, marksMaths, marksEnglish, average are
examples for instance attributes. The attribute avg is used to store the
average of average marks of each student. There is one avg value for
all the students and it should be a class attribute. We use keyword
static to distinguish class attributes.
9. Lesson 1 of 4 Object Oriented Programming in Java
9 of 9
Now modify the two files as follows.
Student.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
static double avg;
}
Test.java
class Test
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.avg=10;
System.out.println(s2.avg);
}
}
This will give 10.0 as the output. The avg variable is common to all
the objects. Therefore following three lines do the same thing.
s1.avg
s2.avg
Student.avg