SlideShare a Scribd company logo
Computer
Programming 2
Lesson 3– OBJECT AND CLASSES
Prepared by: Analyn G. Regaton
OBJECTAND
CLASSES
 Object − have states and behaviors. Example: A dog has states - color,
name, breed as well as behaviors – wagging the tail, barking, eating. An
object is an instance of a class.
 Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type support.
OBJECTS IN
JAVA
 Software objects also have a state and a
behavior. A software object's state is stored in
fields and behavior is shown via methods.
 So in software development, methods operate
on the internal state of an object and the object-
to-object communication is done via methods.
CLASSES IN
JAVA
A class is a blueprint from which individual objects
are created.
Following is a sample of a class.
Example
public class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
OBJECTS
CLASS
CLASSES IN
JAVA
 A class can contain any of the following variable types.
 Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.
 Instance variables − Instance variables are variables
within a class but outside any method. These variables
are initialized when the class is instantiated. Instance
variables can be accessed from inside any method,
constructor or blocks of that particular class.
 Class variables − Class variables are variables declared
within a class, outside any method, with the static
keyword.
CONTRUCTORS
Each time a new object is created, at least one
constructor will be invoked. The main rule of
constructors is that they should have the same name as
the class. A class can have more than one constructor.
Following is an example of a constructor −
public class Puppy {
public Puppy() {
}
public Puppy(String name) {
// This constructor has one parameter, name.
}
}
CREATINGAN
OBJECT
A class provides the blueprints for objects. So basically, an
object is created from a class. In Java, the new keyword is
used to create new objects.
There are three steps when creating an object from a class −
 Declaration − A variable declaration with a variable name
with an object type.
 Instantiation − The 'new' keyword is used to create the
object.
 Initialization − The 'new' keyword is followed by a call to
a constructor. This call initializes the new object.
EXAMPLE
public class Puppy
{
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
If we compile and run the above program, then it will
produce the following result −
Output
Passed Name is :tommy

More Related Content

PDF
Python - object oriented
PPTX
Java Inheritance | Java Course
PPTX
Object oriented programming
PPTX
Inheritance in java
PPTX
Class properties
PPT
11 Using classes and objects
PPTX
Chapter 7 java
Python - object oriented
Java Inheritance | Java Course
Object oriented programming
Inheritance in java
Class properties
11 Using classes and objects
Chapter 7 java

What's hot (20)

PPTX
Lecture 4
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
OOP Basic Concepts
PPTX
object oriented programming using c++
PPTX
Packages namespace instance
PPTX
Mutable and immutable classes
PPTX
Pj01 x-classes and objects
PPSX
Dr. Rajeshree Khande : Java Inheritance
PPT
Classes cpp intro thomson bayan college
PPTX
Java inheritance
PDF
Java Programming Paradigms Chapter 1
PPTX
Inheritance
PPTX
Chapter 07 inheritance
PPTX
Chapter 8.2
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
Introducing classes
PPTX
Inheritance in java
PPT
OOPs Lecture 2
DOCX
Keyword of java
PPTX
[OOP - Lec 20,21] Inheritance
Lecture 4
encapsulation, inheritance, overriding, overloading
OOP Basic Concepts
object oriented programming using c++
Packages namespace instance
Mutable and immutable classes
Pj01 x-classes and objects
Dr. Rajeshree Khande : Java Inheritance
Classes cpp intro thomson bayan college
Java inheritance
Java Programming Paradigms Chapter 1
Inheritance
Chapter 07 inheritance
Chapter 8.2
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Introducing classes
Inheritance in java
OOPs Lecture 2
Keyword of java
[OOP - Lec 20,21] Inheritance
Ad

Similar to Computer programming 2 Lesson 3 (20)

PDF
Lect 1-java object-classes
PPTX
Unit3 part1-class
DOCX
Computer Programming 2
PDF
principles of proramming language in cppg
PPTX
classes-objects in oops java-201023154255.pptx
PDF
Chapter- 2 Introduction to Class and Object.pdf
PPTX
Ch-2ppt.pptx
PPTX
Is2215 lecture2 student(2)
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
Object oriented javascript
PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
PPTX
classes and objects of python object oriented
PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
PPTX
Lecture 5.pptx
PPTX
python.pptx
PPTX
JavaScript OOPS Implimentation
DOCX
Object oriented programming tutorial
PPTX
UNIT - IIInew.pptx
Lect 1-java object-classes
Unit3 part1-class
Computer Programming 2
principles of proramming language in cppg
classes-objects in oops java-201023154255.pptx
Chapter- 2 Introduction to Class and Object.pdf
Ch-2ppt.pptx
Is2215 lecture2 student(2)
UNIT 3- Java- Inheritance, Multithreading.pptx
Object oriented javascript
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
classes and objects of python object oriented
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
IPP-M5-C1-Classes _ Objects python -S2.pptx
Lecture 5.pptx
python.pptx
JavaScript OOPS Implimentation
Object oriented programming tutorial
UNIT - IIInew.pptx
Ad

More from MLG College of Learning, Inc (20)

PPTX
PPTX
PC111-lesson1.pptx
PPTX
PC LEESOON 6.pptx
PPTX
PC 106 PPT-09.pptx
PPTX
PPTX
PPTX
PPTX
PC 106 Slide no.02
PPTX
PPTX
PPTX
PC 106 Slide 1.pptx
PDF
Db2 characteristics of db ms
PDF

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?

Computer programming 2 Lesson 3

  • 1. Computer Programming 2 Lesson 3– OBJECT AND CLASSES Prepared by: Analyn G. Regaton
  • 2. OBJECTAND CLASSES  Object − have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.  Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
  • 3. OBJECTS IN JAVA  Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.  So in software development, methods operate on the internal state of an object and the object- to-object communication is done via methods.
  • 4. CLASSES IN JAVA A class is a blueprint from which individual objects are created. Following is a sample of a class. Example public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } } OBJECTS CLASS
  • 5. CLASSES IN JAVA  A class can contain any of the following variable types.  Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.  Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.  Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
  • 6. CONTRUCTORS Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. Following is an example of a constructor − public class Puppy { public Puppy() { } public Puppy(String name) { // This constructor has one parameter, name. } }
  • 7. CREATINGAN OBJECT A class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects. There are three steps when creating an object from a class −  Declaration − A variable declaration with a variable name with an object type.  Instantiation − The 'new' keyword is used to create the object.  Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  • 8. EXAMPLE public class Puppy { public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public static void main(String []args) { // Following statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } } If we compile and run the above program, then it will produce the following result − Output Passed Name is :tommy