SlideShare a Scribd company logo
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
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
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[])
{
}
}
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.
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.
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.
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.
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.
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

More Related Content

PDF
Methods in Java
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PPT
4 Classes & Objects
PPTX
Classes and objects
PPT
Basic Java Part 1
PDF
Python - object oriented
PPTX
Classes and objects
PPTX
Static keyword ppt
Methods in Java
Python Class | Python Programming | Python Tutorial | Edureka
4 Classes & Objects
Classes and objects
Basic Java Part 1
Python - object oriented
Classes and objects
Static keyword ppt

What's hot (20)

PDF
Classes and objects
PDF
Introduction to Classes and Objects in Java - Edufect
PPT
2 lesson 2 object oriented programming in c++
PPT
Classes cpp intro thomson bayan college
PPTX
Object Oriented Programming Using C++
PPTX
Classes and objects in c++
PPTX
object oriented programming using c++
PPTX
Chapter 7 java
PPT
Java Notes
PDF
ITFT-Classes and object in java
PDF
Core java complete notes - Contact at +91-814-614-5674
PDF
Classes and objects in java
PPT
Lect 1-class and object
PPTX
C++ classes
PPTX
Java class,object,method introduction
PPTX
Constructor in java
PPTX
6. static keyword
PDF
Lect 1-java object-classes
PPTX
C++ And Object in lecture3
PDF
CLASS & OBJECT IN JAVA
Classes and objects
Introduction to Classes and Objects in Java - Edufect
2 lesson 2 object oriented programming in c++
Classes cpp intro thomson bayan college
Object Oriented Programming Using C++
Classes and objects in c++
object oriented programming using c++
Chapter 7 java
Java Notes
ITFT-Classes and object in java
Core java complete notes - Contact at +91-814-614-5674
Classes and objects in java
Lect 1-class and object
C++ classes
Java class,object,method introduction
Constructor in java
6. static keyword
Lect 1-java object-classes
C++ And Object in lecture3
CLASS & OBJECT IN JAVA
Ad

Viewers also liked (15)

PDF
Exercises for Two Dimensional Geometric Transformations
PDF
Linked List Implementation of Stack in C
PDF
Flood Filling Algorithm in C
PDF
Computing the Area of a Polygon
PPTX
Improving the accuracy of k-means algorithm using genetic algorithm
PDF
Digital Differential Analyzer Line Drawing Algorithm in C
PDF
Exercises for Convexity of Polygons
DOCX
Implementation of k-means clustering algorithm in C
PDF
Access modifiers
PDF
Principles of Object Oriented Programming
PDF
Linked List Implementation of Deque in C
DOCX
Boundary Fill Algorithm in C
PPTX
Digital Differential Analyzer Line Drawing Algorithm
PDF
Linked List Implementation of Queue in C
Exercises for Two Dimensional Geometric Transformations
Linked List Implementation of Stack in C
Flood Filling Algorithm in C
Computing the Area of a Polygon
Improving the accuracy of k-means algorithm using genetic algorithm
Digital Differential Analyzer Line Drawing Algorithm in C
Exercises for Convexity of Polygons
Implementation of k-means clustering algorithm in C
Access modifiers
Principles of Object Oriented Programming
Linked List Implementation of Deque in C
Boundary Fill Algorithm in C
Digital Differential Analyzer Line Drawing Algorithm
Linked List Implementation of Queue in C
Ad

Similar to Classes and objects (20)

PPTX
Java-U1-C_1.2.pptx its all about the java
DOCX
javaopps concepts
PDF
principles of proramming language in cppg
PPT
packages and interfaces
DOCX
Computer Programming 2
DOCX
Object oriented programming tutorial
PPTX
Introduction to java programming
PPTX
classes-objects in oops java-201023154255.pptx
PPT
Oop java
PDF
Chapter- 2 Introduction to Class and Object.pdf
PPT
A457405934_21789_26_2018_Inheritance.ppt
PDF
JAVA PPT -2 BY ADI.pdf
PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
PPTX
Unit No 2 Objects and Classes.pptx
PPT
PDF
Object Oriented Programming using JAVA Notes
PDF
Unit 2 notes.pdf
PPT
slides 01.ppt
PPTX
Object Oriented Programming 02b: Classes
PPT
4.Packages_m1.ppt
Java-U1-C_1.2.pptx its all about the java
javaopps concepts
principles of proramming language in cppg
packages and interfaces
Computer Programming 2
Object oriented programming tutorial
Introduction to java programming
classes-objects in oops java-201023154255.pptx
Oop java
Chapter- 2 Introduction to Class and Object.pdf
A457405934_21789_26_2018_Inheritance.ppt
JAVA PPT -2 BY ADI.pdf
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
Unit No 2 Objects and Classes.pptx
Object Oriented Programming using JAVA Notes
Unit 2 notes.pdf
slides 01.ppt
Object Oriented Programming 02b: Classes
4.Packages_m1.ppt

More from Kasun Ranga Wijeweera (20)

PDF
Decorator Design Pattern in C#
PDF
Singleton Design Pattern in C#
PDF
Introduction to Design Patterns
PPTX
Algorithms for Convex Partitioning of a Polygon
PDF
Geometric Transformations II
PDF
Geometric Transformations I
PDF
Introduction to Polygons
PDF
Bresenham Line Drawing Algorithm
PDF
Digital Differential Analyzer Line Drawing Algorithm
PDF
Loops in Visual Basic: Exercises
PDF
Conditional Logic: Exercises
PDF
Getting Started with Visual Basic Programming
PDF
CheckBoxes and RadioButtons
PDF
Variables in Visual Basic Programming
PDF
Loops in Visual Basic Programming
PDF
Conditional Logic in Visual Basic Programming
PDF
Assignment for Variables
PDF
Assignment for Factory Method Design Pattern in C# [ANSWERS]
PDF
Assignment for Events
PDF
Mastering Arrays Assignment
Decorator Design Pattern in C#
Singleton Design Pattern in C#
Introduction to Design Patterns
Algorithms for Convex Partitioning of a Polygon
Geometric Transformations II
Geometric Transformations I
Introduction to Polygons
Bresenham Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
Loops in Visual Basic: Exercises
Conditional Logic: Exercises
Getting Started with Visual Basic Programming
CheckBoxes and RadioButtons
Variables in Visual Basic Programming
Loops in Visual Basic Programming
Conditional Logic in Visual Basic Programming
Assignment for Variables
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Events
Mastering Arrays Assignment

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
Jenkins: An open-source automation server powering CI/CD Automation
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administraation Chapter 3
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Forouzan Book Information Security Chaper - 1
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
DOCX
The Five Best AI Cover Tools in 2025.docx
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
Become an Agentblazer Champion Challenge
PPT
JAVA ppt tutorial basics to learn java programming
PDF
Comprehensive Salesforce Implementation Services.pdf
PPTX
Save Business Costs with CRM Software for Insurance Agents
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PTS Company Brochure 2025 (1).pdf.......
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Jenkins: An open-source automation server powering CI/CD Automation
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
How Creative Agencies Leverage Project Management Software.pdf
Forouzan Book Information Security Chaper - 1
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ManageIQ - Sprint 268 Review - Slide Deck
The Five Best AI Cover Tools in 2025.docx
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
The Role of Automation and AI in EHS Management for Data Centers.pdf
Become an Agentblazer Champion Challenge
JAVA ppt tutorial basics to learn java programming
Comprehensive Salesforce Implementation Services.pdf
Save Business Costs with CRM Software for Insurance Agents
Upgrade and Innovation Strategies for SAP ERP Customers

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: [email protected] 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