Professional Documents
Culture Documents
IT 103 Module 3
IT 103 Module 3
MODULE: 3
OVERVIEW
Java – Objects and Classes
What is an Object? Create an Object
Characteristics of Object Anonymous Object
What is a Class? Creating Multiple Objects
3 Ways to Initialize Object Real World Example
MODULE OUTCOMES
At the end of this module, the students must have:
defined and differentiated Objects and Classes;
distinguished the Characteristics of Object, Creation and Initialization;
created a program with Objects and Classes using Real World example;
INTRODUCTION
Classes are a fundamental structure of any object-oriented programming language and Java is no
exception. It is a blueprint or structural definition of a design upon which objects are built. Objects are
instances of these designs. Analogically, if classes are a design map of a building structure, objects are
the building built upon it. So, in a way, classes are generally an abstraction of a real-world entity. It is
abstract because it does not give a form/shape, but a definition on how concrete classes would look.
Because, in programming, we represents some data, classes that exactly fit that role, it is reasonable
to call it an Abstract Data Type. And, by the way, objects are the concrete entity built upon that
abstraction.
DISCUSSION
What is an object in Java
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table,
car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is
the banking system.
For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It is used to
write, so writing is its behavior.
An object is an instance of a class. A class is a template or blueprint from which objects are
created. So, an object is the instance(result) of a class.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
Method in Java
In Java, a method is like a function which is used to expose the behavior of an object.
Advantage of Method
Code Reusability
Code Optimization
File: Student.java
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Output:
0
null
We can have multiple classes in different Java files or single Java file. If you define multiple classes in a
single Java source file, it is a good idea to save the file name with the class name which has main()
method.
File: TestStudent1.java
//Java Program to demonstrate having the main method in
//another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Output:
0
null
File: TestStudent2.java
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
}
}
Output:
101 Sonoo
We can also create multiple objects and store information in it through reference variable.
File: TestStudent3.java
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
Output:
101 Sonoo
102 Amit
File: TestStudent4.java
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
Output:
111 Karan
222 Aryan
As you can see in the above figure, object gets the memory in heap memory area. The reference
variable refers to the object allocated in the heap memory area. Here, s1 and s2 both are reference
variables that refer to the objects allocated in memory.
File: TestEmployee.java
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}
Output:
101 ajeet 45000.0
102 irfan 25000.0
103 nakul 55000.0
File: TestRectangle1.java
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){System.out.println(length*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Output:
55
45
Anonymous object
Anonymous simply means nameless. An object which has no reference is known as an anonymous
object. It can be used at the time of object creation only.
If you have to use an object only once, an anonymous object is a good approach. For example:
new Calculation();//anonymous object
Output:
Factorial is 120
Output:
55
45
Output:
832345 Ankit 1000.0
Balance is: 1000.0
40000.0 deposited
Balance is: 41000.0
15000.0 withdrawn
Balance is: 26000.0
Using Java.util Package in Java (Scanner)
Java Program to Calculate Area of Rectangle
Program 1:
User would provide the length and width values during execution of the program and the area would
be calculated based on the provided values.
import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
Program 2:
In the above program, user would be asked to provide the length and width values. If you do not need
user interaction and simply want to specify the values in program, refer the below program.
class AreaOfRectangle2 {
public static void main (String[] args)
{
double length = 4.5;
double width = 8.0;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
Output:
Activity No. 1
Name: Date:
Year and Section: Signature:
(20 points) Create a Java Program that can calculate area of a Square.
Given:
Take Note:
Class Name should be your Family Name, uppercase first letter. Example: Perucho
Unit of measurement is in centimeter(cm). The answer should be in cm. (no unit minus 5)
It can be either of two program:
1) Program 1: Prompting user for entering the side of the square
2) Program 2: Side of the square is specified in the program’ s source code.
Activity No. 2
Name: Date:
Year and Section: Signature:
(20 points) Write an essay about Java Objects and Classes reffering to Real World Entities. Minimum
of 150 words maximum of 300 words.
SUMMARY
Objects and classes are such a fundamental concept that, when you talk about this subject, you tend to
include every aspect of the object-oriented paradigm. Objects and classes are just a veil, a simple
definition, such as one is a structure and another is an example of that structure. It iss just an
oversimplification. Once you penetrate this idea and have a peek through the veil, a whole new world
emerges, the world of object-oriented technology that is definitely more than Java. As a result, we can
almost say, if Object-Oriented Technology is a class, Java is an object of it.
Put a check on the topic whether you can understand it on your own, need more reference material, or
if you can’t understand.
SELF ASSESSMENT
I CAN DO IT WITH
I CAN DO IT ON MY THE HELP OF A I CANNOT
OWN. REFERENCE UNDERSTAND.
MATERIAL.
Java – Objects and
Classes
Name: Date:
Year and Section: Signature:
REFERENCES
1. CADENHEAD, R. (2018). SAMS TEACH YOURSELF JAVA IN 21 DAYS. 800 EAST 96TH
STREET, INDIANAPOLIS, INDIANA 46240. PEARSON EDUCATION.
2. ECK,D.(2017). INTRODUCTION TO PROGRAMMING USING JAVA. HOBART AND WILLIAM
SMITH COLLEGES, GENEVA, NY 14456
3. MEDLEY, R. (2018, JUNE 7). NETBEANS. RETRIEVED FROM NETBEANS.ORG:
HTTPS://EDU.NETBEANS.ORG/CONTRIB/JEDI/INTRO-TO-PROGRAMMING- 1/COURSE-
OUTLINE.PDF
4. SHICHT, H. (2018, JUNE 7). COMPUTER_FUNDAMENTALS. RETRIEVED FROM
TUTORIALSPOINT.COM:
HTTPS://WWW.TUTORIALSPOINT.COM/COMPUTER_FUNDAMENTALS/COMPUTER_NUMBER
_SYSTEM.HTM
SUGGESTED READINGS
1. https://www.developer.com/design/understanding-java-objects-and-java-classes/#:~:text=Overview
%20of%20Java%20Objects&text=Objects%20are%20created%20with%20the%20new
%20keyword.&text=An%20object%20is%20a%20distinct,objects%20of%20the%20same%20class.
2. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
3. https://www.javatpoint.com/object-and-class-in-java
4. https://www.tutorialspoint.com/java/java_object_classes.htm