Object-Oriented Programming in Java

 
O
b
j
e
c
t
 
O
r
i
e
n
t
e
d
P
r
o
g
r
a
m
m
i
n
g
 
i
n
 
J
a
v
a
 
C
h
a
p
t
e
r
 
2
 
I
n
 
T
h
i
s
 
C
h
a
p
t
e
r
 
y
o
u
 
w
i
l
l
 
l
e
a
r
n
 
I
n
 
T
h
i
s
 
C
h
a
p
t
e
r
 
y
o
u
 
w
i
l
l
 
l
e
a
r
n
What object-oriented programming is
Objects and classes
To Investigating inheritance and interfaces
To Designing programs with objects
 
O
b
j
e
c
t
-
o
r
i
e
n
t
e
d
 
p
r
o
g
r
a
m
m
i
n
g
l
a
n
g
u
a
g
e
s
 
 
Object-oriented programming is modelled on
how, in the real world, objects are often made
up of many kinds of smaller objects.
 
W
h
a
t
 
i
s
 
a
 
C
l
a
s
s
?
 
 
 
Class is code that defines the behavior of a
Java programming element called an object.
 
W
h
a
t
 
i
s
 
a
n
 
O
b
j
e
c
t
?
 
 
Is anything that is visible or tangible and is
relatively stable in form, a thing, person, or
matter to which thought or action is directed.
 
C
h
a
r
a
c
t
e
r
i
s
t
i
c
s
 
o
f
 
a
n
 
O
b
j
e
c
t
 
I
d
e
n
t
i
t
y
T
y
p
e
S
t
a
t
e
B
e
h
a
v
i
o
r
 
I
d
e
n
t
i
t
y
 
Every object in an object-oriented
program has an 
identity. In other words,
every occurrence of a particular type of
object — called an instance — can be
distinguished from every other
occurrence of the same type of object,
as well as from objects of other types.
 
T
y
p
e
 
In Java, classes define types. Therefore,
when you create an object from a type, you
are saying 
that the object is of the type
specified by the class.
 
S
t
a
t
e
 
consists of any data that the object might be
keeping track of
 
The combination of the values for all the
attributes of an object is called 
the object’s
state.
 
B
e
h
a
v
i
o
r
 
Consists of actions that the object can
perform.
 
Means that 
they can do things. Like state, the
specific behavior of an object depends on its
type, but unlike state, behavior is not different
for each instance of a type.
 
C
r
e
a
t
i
n
g
 
o
b
j
e
c
t
 
f
r
o
m
 
a
 
c
l
a
s
s
 
 
 
ClassName variableName = new ClassName();
 
T
h
e
 
p
r
e
v
i
o
u
s
 
s
t
a
t
e
m
e
n
t
a
c
t
u
a
l
l
y
 
d
o
e
s
 
t
h
r
e
e
 
t
h
i
n
g
s
:
 
It creates a variable named myClass1
Object that can be used to hold objects
created from the Class1 class. At this point,
no object has been created — just a
variable that can be used to store objects
 
It creates a new object in memory from the
Class1 class.
It assigns this newly created object to the
myClass1Object variable. That way, you
can use the myClassObject variable to refer
to the object that was created.
 
 
E
x
a
m
p
l
e
 
public class Greeter {
   
 
public Greeter() {
 
 
}
   public void sayHello(){
   
 
System.out.println("Hello! Welcome to Java Programming!");
    
 
}
    }
 
public class Hello {
public static void main(String args[] ){
 
Greeter greeter = new Greeter(); //
Instantiation
 
greeter.sayHello():
 
}
}
 
C
h
a
l
l
e
n
g
e
:
 
 
Write any statement in the constructor and
see what happen.
 
public class Greeter
{
   public void sayHello()
 {
 
JOptionPane.showMessageDialog(null,
"Hello, World!", "Greeter",
JOptionPane.INFORMATION_MESSAGE);
 }
}
 
I
m
p
o
r
t
i
n
g
 
J
a
v
a
 
A
P
I
 
C
l
a
s
s
e
s
 
The purpose of the 
import statement is to let
the compiler know that the program is using a
class that’s defined by the Java API called
JOptionPane.
 
R
u
l
e
s
 
f
o
r
 
w
o
r
k
i
n
g
 
w
i
t
h
 
i
m
p
o
r
t
s
t
a
t
e
m
e
n
t
s
 
Import
 statements must appear at the
beginning of the class file, before any class
declarations
 
You can include as many 
import statements
as are necessary to import all the classes
used by your program
 
You can import all the classes in a
particular package by listing the pack
-age
name followed by an asterisk wildcard, like
this:
import javax.swing.*;
 
Because many programs use the classes that
are contained in the 
java.lang package, you
don’t have to import that package. Instead,
those classes are automatically available to
all programs. The System class is defined in
the java.lang package. As a result, you don’t
have to provide an import statement to use
this class
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
 
import
 statements are required
in your program whenever you
are incorporating an API in your
program.
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
in order to implement the
greeter class you need to call
it inside another class and
create an object.
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
The state of an object
consists of any data that the
object might be keeping track
of
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
In Object Oriented
Programming you need to
change everything when one of
the component of your program
needed to be modified
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
Class is code that defines
the behavior of a Java
programming element called
an object
.
 
Congratulations.
Well done
Slide Note
Embed
Share

This chapter delves into the principles of object-oriented programming in Java. Exploring key concepts such as classes, objects, inheritance, and polymorphism, the text provides a foundational understanding of how Java utilizes OOP to create robust and efficient programs. Through practical examples and explanations, readers can grasp the essence of OOP and its significance in Java programming.

  • Java programming
  • Object-oriented
  • Classes
  • Inheritance
  • Polymorphism

Uploaded on Feb 16, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter 2 Object Oriented Programming in Java

  2. In This Chapter you will learn In This Chapter you will learn What object-oriented programming is Objects and classes To Investigating inheritance and interfaces To Designing programs with objects

  3. Object-oriented programming languages Object-oriented programming is modelled on how, in the real world, objects are often made up of many kinds of smaller objects.

  4. What is a Class? Class is code that defines the behavior of a Java programming element called an object.

  5. What is an Object? Is anything that is visible or tangible and is relatively stable in form, a thing, person, or matter to which thought or action is directed.

  6. Characteristics of an Object Identity Type State Behavior

  7. Identity Every object in an object-oriented program has an identity. In other words, every occurrence of a particular type of object called an instance can be distinguished from every other occurrence of the same type of object, as well as from objects of other types.

  8. Type In Java, classes define types. Therefore, when you create an object from a type, you are saying that the object is of the type specified by the class.

  9. State consists of any data that the object might be keeping track of The combination of the values for all the attributes of an object is called the object s state.

  10. Behavior Consists of actions that the object can perform. Means that they can do things. Like state, the specific behavior of an object depends on its type, but unlike state, behavior is not different for each instance of a type.

  11. Creating object from a class ClassName variableName = new ClassName();

  12. The previous statement actually does three things: It creates a variable named myClass1 Object that can be used to hold objects created from the Class1 class. At this point, no object has been created just a variable that can be used to store objects

  13. It creates a new object in memory from the Class1 class. It assigns this newly created object to the myClass1Object variable. That way, you can use the myClassObject variable to refer to the object that was created.

  14. Example public class Greeter { public Greeter() { } public void sayHello(){ System.out.println("Hello! Welcome to Java Programming!"); } }

  15. public class Hello { public static void main(String args[] ){ Greeter greeter = new Greeter(); //Instantiation greeter.sayHello(): } }

  16. Challenge: Write any statement in the constructor and see what happen.

  17. public class Greeter{ public void sayHello() { JOptionPane.showMessageDialog(null,"Hello, World!", "Greeter", JOptionPane.INFORMATION_MESSAGE); } }

  18. Importing Java API Classes The purpose of the import statement is to let the compiler know that the program is using a class that s defined by the Java API called JOptionPane.

  19. Rules for working with import statements Import statements must appear at the beginning of the class file, before any class declarations You can include as many import statements as are necessary to import all the classes used by your program

  20. You can import all the classes in a particular package by listing the pack-age name followed by an asterisk wildcard, like this: import javax.swing.*;

  21. Because many programs use the classes that are contained in the java.lang package, you don t have to import that package. Instead, those classes are automatically available to all programs. The System class is defined in the java.lang package. As a result, you don t have to provide an import statement to use this class

  22. Activity Time

  23. True or False import statements are required in your program whenever you are incorporating an API in your program.

  24. True or False in order to implement the greeter class you need to call it inside another class and create an object.

  25. True or False The state of an object consists of any data that the object might be keeping track of

  26. True or False In Object Oriented Programming you need to change everything when one of the component of your program needed to be modified

  27. True or False Class is code that defines the behavior of a Java programming element called an object.

  28. Congratulations. Well done

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#