SlideShare a Scribd company logo
Objects and Classes
Java Methods
Object-Oriented Programming
and Data Structures
3-2
Objectives:
• See an example of a GridWorld program,
written in OOP style, and discuss the types of
objects used in it
• Learn about the general structure of a class,
its fields, constructors, and methods
• Get a feel for how objects are created and
how to call their methods
• Learn a little about inheritance in OOP
3-3
OOP
• An OO program models the application as a
world of interacting objects.
• An object can create other objects.
• An object can call another object’s (and its
own) methods (that is, “send messages”).
• An object has data fields, which hold values
that can change while the program is running.
3-4
Objects
• Can model real-world objects
• Can represent GUI (Graphical User
Interface) components
• Can represent software entities (events,
files, images, etc.)
• Can represent abstract concepts (for
example, rules of a game, a location on a
grid, etc.)
3-5
Objects in the Bug Runner program
Grid
Control panel
Bugs, flowers,
and rocks
Message
display
GridWorld
window
Locations (in
the grid)
Buttons, slider
Menu bar,
menus
Scroll bar
Colors
3-6
Classes and Objects
• A class is a piece of the program’s source
code that describes a particular type of
objects. OO programmers write class
definitions.
• An object is called an instance of a class.
A program can create and use more than
one object (instance) of the same class.
3-7
Class Object
• A blueprint for
objects of a
particular type
• Defines the
structure (number,
types) of the
attributes
• Defines available
behaviors of its
objects
Attributes
Behaviors
3-8
Class: Car Object: a car
Attributes:
String model
Color color
int numPassengers
double amountOfGas
Behaviors:
Add/remove a passenger
Get the tank filled
Report when out of gas
Attributes:
model = "Mustang"
color = Color.YELLOW
numPassengers = 0
amountOfGas = 16.5
Behaviors:
3-9
Class vs. Object
• A piece of the
program’s source
code
• Written by a
programmer
• An entity in a
running program
• Created when the
program is running
(by the main method
or a constructor or
another method)
3-10
Class vs. Object
• Specifies the
structure (the
number and types)
of its objects’
attributes — the
same for all of its
objects
• Specifies the
possible behaviors
of its objects
• Holds specific
values of attributes;
these values can
change while the
program is running
• Behaves
appropriately when
called upon
3-11
Bug
Occupies a location in
the grid
Moves
Turns
Acts: moves if it can,
otherwise turns.
Actor
Grid
Location
CRC Card
• A preliminary description of a class at the
initial stage of program design
Collaborators
Class
Responsibilities
3-12
Classes and Source Files
• Each class is stored in a separate file
• The name of the file must be the same as the
name of the class, with the extension .java
public class Car
{
...
}
Car.java By convention, the
name of a class
(and its source file)
always starts with
a capital letter.
(In Java, all names are case-sensitive.)
3-13
Libraries
• Java programs are usually not written from
scratch.
• There are hundreds of library classes for all
occasions.
• Library classes are organized into packages.
For example:
java.util — miscellaneous utility classes
java.awt — windowing and graphics toolkit
javax.swing — GUI development package
3-14
import
• Full library class names include the package
name. For example:
java.awt.Color
javax.swing.JButton
• import statements at the top of the source file
let you refer to library classes by their short
names:
import javax.swing.JButton;
...
JButton go = new JButton("Go");
Fully-qualified
name
3-15
import (cont’d)
• You can import names for all the classes in a
package by using a wildcard .*:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
• java.lang is imported automatically into all
classes; defines System, Math, Object,
String, and other commonly used classes.
Imports all classes
from awt, awt.event,
and swing packages
3-16
import (cont’d)
• GridWorld has its own library (gridworld.jar)
• You need to tell the compiler where to find
GridWorld classes:
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
...
import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
...
3-17
public class SomeClass
• Fields
• Constructors
• Methods
}
Attributes / variables that define the
object’s state; can hold numbers,
characters, strings, other objects
Procedures for constructing
a new object of this class
and initializing its fields
Actions that an object
of this class can take
(behaviors)
{
Class header
SomeClass.java
import ... import statements
3-18
public class Actor
{
private Grid<Actor> grid;
private Location location;
private int direction;
private Color color;
public Actor()
{
color = Color.BLUE;
direction = Location.NORTH;
grid = null;
location = null;
}
...
public void moveTo(Location newLocation)
{
...
}
...
}
Fields
Constructor(s)
Methods
3-19
Fields
• A.k.a. instance variables
• Constitute “private memory” of an object
• Each field has a data type (int, double, String,
Color, Location, etc.)
• Each field has a name given by the
programmer
3-20
private [static] [final] datatype name;
Fields (cont’d)
Usually
private
May be present:
means the field
is a constant
int, double, etc., or an
object: String, Location,
Color
You name it!
May be present:
means the field is
shared by all
objects in the class
private Location location;
3-21
Constructors
• Short procedures for creating objects of a
class
• Always have the same name as the class
• Initialize the object’s fields
• May take parameters
• A class may have several constructors that
differ in the number and/or types of their
parameters
3-22
Constructors (cont’d)
public class Location
{
private int row;
private int col;
...
public Location (int r, int c)
{
row = r;
col = c;
}
...
}
The name of a constructor
is always the same as the
name of the class
A constructor can take parameters
Initializes fields
3-23
Constructors (cont’d)
public class Location
{
...
public Location (int r, int c)
{
...
}
...
}
// BugRunner.java
...
Location loc = new Location(3, 5);
...
An object is created with
the new operator
The number, order, and
types of parameters must
match
Constructor
3-24
Constructors (cont’d)
JButton go = new JButton("Go");
3-25
Methods
• Call them for a particular object:
ActorWorld world = new ActorWorld();
Bug bob = new Bug();
world.add (new Location(3, 5), bob);
bob.move( );
bob.turn( );
3-26
Methods (cont’d)
• The number and types of parameters (a.k.a.
arguments) passed to a method must match
method’s parameters:
g.drawString ("Welcome", 120, 50);
public void drawString ( String msg, int x, int y )
{
...
}
3-27
Methods (cont’d)
• A method can return a value to the caller
• The keyword void in the method’s header
indicates that the method does not return any
value
public void drawString ( ... )
{
...
}
3-28
Encapsulation and
Information Hiding
• A class interacts with other classes only
through constructors and public methods
• Other classes do not need to know the
mechanics (implementation details) of a class
to use it effectively
• Encapsulation facilitates team work and
program maintenance (making changes to
the code)
3-29
Methods (cont’d)
• Constructors and methods can call other
public and private methods of the same class.
• Constructors and methods can call only
public methods of another class.
Class X
private field
private method
Class Y
public method public method
3-30
Inheritance
• In OOP a programmer can create a new class
by extending an existing class
Superclass
(Base class)
Subclass
(Derived class)
subclass extends
superclass
3-31
Inheritance (cont’d)
Bug
UTurnBug
UTurnBug extends
Bug
Actor
Bug extends Actor
3-32
A Subclass...
• inherits fields and methods of its
superclass
• can add new fields and methods
• can redefine (override) a method of the
superclass
• must provide its own constructors, but
calls superclass’s constructors
• does not have direct access to its
superclass’s private fields
3-33
public class UTurnBug extends Bug
{
public UTurnBug (Color bugColor)
{
setColor (bugColor);
}
...
public void turnAround ()
{
turn(); turn(); turn(); turn();
// Or: setDirection (getDirection() + 180);
}
...
}
A new
method
Constructor
3-34
Review:
• Name a few objects used in GridWorld’s
BugRunner.
• Name a few library classes used in
GridWorld.
• What are import statements used for?
• What is a field? A constructor? A method?
• Which operator is used to construct an
object?
3-35
Review (cont’d):
• What is the difference between private and
public methods?
• Why are fields usually private?
• What is inheritance?
• Can a subclass add new fields? New
methods?

More Related Content

What's hot

C++ classes
C++ classesC++ classes
C++ classes
Aayush Patel
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
ahmed hmed
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
Adeel Rasheed
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
class c++
class c++class c++
class c++
vinay chauhan
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
M Vishnuvardhan Reddy
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 

What's hot (20)

C++ classes
C++ classesC++ classes
C++ classes
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
class c++
class c++class c++
class c++
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 

Similar to Ch03

Lecture 4
Lecture 4Lecture 4
Lecture 4
talha ijaz
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
Syed Afaq Shah MACS CP
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
Arc Keepers Solutions
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
Arc Keepers Solutions
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
MIT302 Lesson 2_Advanced Database Systems.pptx
MIT302 Lesson 2_Advanced Database Systems.pptxMIT302 Lesson 2_Advanced Database Systems.pptx
MIT302 Lesson 2_Advanced Database Systems.pptx
elsagalgao
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
4-OOPS.pptx
4-OOPS.pptx4-OOPS.pptx
4-OOPS.pptx
SatyamMishra237306
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
Haitham El-Ghareeb
 
CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)
Dr. Ahmed Al Zaidy
 

Similar to Ch03 (20)

Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
MIT302 Lesson 2_Advanced Database Systems.pptx
MIT302 Lesson 2_Advanced Database Systems.pptxMIT302 Lesson 2_Advanced Database Systems.pptx
MIT302 Lesson 2_Advanced Database Systems.pptx
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
4-OOPS.pptx
4-OOPS.pptx4-OOPS.pptx
4-OOPS.pptx
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)
 

More from AbhishekMondal42

Oss evaluation-certification-oss-financial-advantages
Oss evaluation-certification-oss-financial-advantagesOss evaluation-certification-oss-financial-advantages
Oss evaluation-certification-oss-financial-advantages
AbhishekMondal42
 
Word press 01
Word press 01Word press 01
Word press 01
AbhishekMondal42
 
Word press posts(preview &amp; publish)
Word press posts(preview &amp; publish)Word press posts(preview &amp; publish)
Word press posts(preview &amp; publish)
AbhishekMondal42
 
Word press posts(add , edit , delete post)
Word press posts(add , edit , delete post)Word press posts(add , edit , delete post)
Word press posts(add , edit , delete post)
AbhishekMondal42
 
Word press pages(edit and delete)
Word press pages(edit and delete)Word press pages(edit and delete)
Word press pages(edit and delete)
AbhishekMondal42
 
Word press pages(add)
Word press pages(add)Word press pages(add)
Word press pages(add)
AbhishekMondal42
 
Word press media(add,insert,delete)
Word press media(add,insert,delete)Word press media(add,insert,delete)
Word press media(add,insert,delete)
AbhishekMondal42
 
Word press media library
Word press media libraryWord press media library
Word press media library
AbhishekMondal42
 
Word press widget management
Word press  widget managementWord press  widget management
Word press widget management
AbhishekMondal42
 
Word press view plugins
Word press  view pluginsWord press  view plugins
Word press view plugins
AbhishekMondal42
 
Word press user roles
Word press  user rolesWord press  user roles
Word press user roles
AbhishekMondal42
 
Word press theme management
Word press  theme managementWord press  theme management
Word press theme management
AbhishekMondal42
 
Word press personal profile
Word press  personal profileWord press  personal profile
Word press personal profile
AbhishekMondal42
 
Word press moderate comments
Word press  moderate commentsWord press  moderate comments
Word press moderate comments
AbhishekMondal42
 
Word press install plugins
Word press  install pluginsWord press  install plugins
Word press install plugins
AbhishekMondal42
 
Word press edit users
Word press  edit usersWord press  edit users
Word press edit users
AbhishekMondal42
 
Word press edit tags
Word press  edit tagsWord press  edit tags
Word press edit tags
AbhishekMondal42
 
Word press edit links
Word press  edit linksWord press  edit links
Word press edit links
AbhishekMondal42
 
Word press edit comments
Word press  edit commentsWord press  edit comments
Word press edit comments
AbhishekMondal42
 
Word press delete users
Word press  delete usersWord press  delete users
Word press delete users
AbhishekMondal42
 

More from AbhishekMondal42 (20)

Oss evaluation-certification-oss-financial-advantages
Oss evaluation-certification-oss-financial-advantagesOss evaluation-certification-oss-financial-advantages
Oss evaluation-certification-oss-financial-advantages
 
Word press 01
Word press 01Word press 01
Word press 01
 
Word press posts(preview &amp; publish)
Word press posts(preview &amp; publish)Word press posts(preview &amp; publish)
Word press posts(preview &amp; publish)
 
Word press posts(add , edit , delete post)
Word press posts(add , edit , delete post)Word press posts(add , edit , delete post)
Word press posts(add , edit , delete post)
 
Word press pages(edit and delete)
Word press pages(edit and delete)Word press pages(edit and delete)
Word press pages(edit and delete)
 
Word press pages(add)
Word press pages(add)Word press pages(add)
Word press pages(add)
 
Word press media(add,insert,delete)
Word press media(add,insert,delete)Word press media(add,insert,delete)
Word press media(add,insert,delete)
 
Word press media library
Word press media libraryWord press media library
Word press media library
 
Word press widget management
Word press  widget managementWord press  widget management
Word press widget management
 
Word press view plugins
Word press  view pluginsWord press  view plugins
Word press view plugins
 
Word press user roles
Word press  user rolesWord press  user roles
Word press user roles
 
Word press theme management
Word press  theme managementWord press  theme management
Word press theme management
 
Word press personal profile
Word press  personal profileWord press  personal profile
Word press personal profile
 
Word press moderate comments
Word press  moderate commentsWord press  moderate comments
Word press moderate comments
 
Word press install plugins
Word press  install pluginsWord press  install plugins
Word press install plugins
 
Word press edit users
Word press  edit usersWord press  edit users
Word press edit users
 
Word press edit tags
Word press  edit tagsWord press  edit tags
Word press edit tags
 
Word press edit links
Word press  edit linksWord press  edit links
Word press edit links
 
Word press edit comments
Word press  edit commentsWord press  edit comments
Word press edit comments
 
Word press delete users
Word press  delete usersWord press  delete users
Word press delete users
 

Recently uploaded

ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 

Recently uploaded (20)

ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 

Ch03

  • 1. Objects and Classes Java Methods Object-Oriented Programming and Data Structures
  • 2. 3-2 Objectives: • See an example of a GridWorld program, written in OOP style, and discuss the types of objects used in it • Learn about the general structure of a class, its fields, constructors, and methods • Get a feel for how objects are created and how to call their methods • Learn a little about inheritance in OOP
  • 3. 3-3 OOP • An OO program models the application as a world of interacting objects. • An object can create other objects. • An object can call another object’s (and its own) methods (that is, “send messages”). • An object has data fields, which hold values that can change while the program is running.
  • 4. 3-4 Objects • Can model real-world objects • Can represent GUI (Graphical User Interface) components • Can represent software entities (events, files, images, etc.) • Can represent abstract concepts (for example, rules of a game, a location on a grid, etc.)
  • 5. 3-5 Objects in the Bug Runner program Grid Control panel Bugs, flowers, and rocks Message display GridWorld window Locations (in the grid) Buttons, slider Menu bar, menus Scroll bar Colors
  • 6. 3-6 Classes and Objects • A class is a piece of the program’s source code that describes a particular type of objects. OO programmers write class definitions. • An object is called an instance of a class. A program can create and use more than one object (instance) of the same class.
  • 7. 3-7 Class Object • A blueprint for objects of a particular type • Defines the structure (number, types) of the attributes • Defines available behaviors of its objects Attributes Behaviors
  • 8. 3-8 Class: Car Object: a car Attributes: String model Color color int numPassengers double amountOfGas Behaviors: Add/remove a passenger Get the tank filled Report when out of gas Attributes: model = "Mustang" color = Color.YELLOW numPassengers = 0 amountOfGas = 16.5 Behaviors:
  • 9. 3-9 Class vs. Object • A piece of the program’s source code • Written by a programmer • An entity in a running program • Created when the program is running (by the main method or a constructor or another method)
  • 10. 3-10 Class vs. Object • Specifies the structure (the number and types) of its objects’ attributes — the same for all of its objects • Specifies the possible behaviors of its objects • Holds specific values of attributes; these values can change while the program is running • Behaves appropriately when called upon
  • 11. 3-11 Bug Occupies a location in the grid Moves Turns Acts: moves if it can, otherwise turns. Actor Grid Location CRC Card • A preliminary description of a class at the initial stage of program design Collaborators Class Responsibilities
  • 12. 3-12 Classes and Source Files • Each class is stored in a separate file • The name of the file must be the same as the name of the class, with the extension .java public class Car { ... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)
  • 13. 3-13 Libraries • Java programs are usually not written from scratch. • There are hundreds of library classes for all occasions. • Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — GUI development package
  • 14. 3-14 import • Full library class names include the package name. For example: java.awt.Color javax.swing.JButton • import statements at the top of the source file let you refer to library classes by their short names: import javax.swing.JButton; ... JButton go = new JButton("Go"); Fully-qualified name
  • 15. 3-15 import (cont’d) • You can import names for all the classes in a package by using a wildcard .*: import java.awt.*; import java.awt.event.*; import javax.swing.*; • java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages
  • 16. 3-16 import (cont’d) • GridWorld has its own library (gridworld.jar) • You need to tell the compiler where to find GridWorld classes: import info.gridworld.grid.Grid; import info.gridworld.grid.Location; ... import info.gridworld.actor.Bug; import info.gridworld.actor.Flower; ...
  • 17. 3-17 public class SomeClass • Fields • Constructors • Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Procedures for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import ... import statements
  • 18. 3-18 public class Actor { private Grid<Actor> grid; private Location location; private int direction; private Color color; public Actor() { color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; } ... public void moveTo(Location newLocation) { ... } ... } Fields Constructor(s) Methods
  • 19. 3-19 Fields • A.k.a. instance variables • Constitute “private memory” of an object • Each field has a data type (int, double, String, Color, Location, etc.) • Each field has a name given by the programmer
  • 20. 3-20 private [static] [final] datatype name; Fields (cont’d) Usually private May be present: means the field is a constant int, double, etc., or an object: String, Location, Color You name it! May be present: means the field is shared by all objects in the class private Location location;
  • 21. 3-21 Constructors • Short procedures for creating objects of a class • Always have the same name as the class • Initialize the object’s fields • May take parameters • A class may have several constructors that differ in the number and/or types of their parameters
  • 22. 3-22 Constructors (cont’d) public class Location { private int row; private int col; ... public Location (int r, int c) { row = r; col = c; } ... } The name of a constructor is always the same as the name of the class A constructor can take parameters Initializes fields
  • 23. 3-23 Constructors (cont’d) public class Location { ... public Location (int r, int c) { ... } ... } // BugRunner.java ... Location loc = new Location(3, 5); ... An object is created with the new operator The number, order, and types of parameters must match Constructor
  • 25. 3-25 Methods • Call them for a particular object: ActorWorld world = new ActorWorld(); Bug bob = new Bug(); world.add (new Location(3, 5), bob); bob.move( ); bob.turn( );
  • 26. 3-26 Methods (cont’d) • The number and types of parameters (a.k.a. arguments) passed to a method must match method’s parameters: g.drawString ("Welcome", 120, 50); public void drawString ( String msg, int x, int y ) { ... }
  • 27. 3-27 Methods (cont’d) • A method can return a value to the caller • The keyword void in the method’s header indicates that the method does not return any value public void drawString ( ... ) { ... }
  • 28. 3-28 Encapsulation and Information Hiding • A class interacts with other classes only through constructors and public methods • Other classes do not need to know the mechanics (implementation details) of a class to use it effectively • Encapsulation facilitates team work and program maintenance (making changes to the code)
  • 29. 3-29 Methods (cont’d) • Constructors and methods can call other public and private methods of the same class. • Constructors and methods can call only public methods of another class. Class X private field private method Class Y public method public method
  • 30. 3-30 Inheritance • In OOP a programmer can create a new class by extending an existing class Superclass (Base class) Subclass (Derived class) subclass extends superclass
  • 32. 3-32 A Subclass... • inherits fields and methods of its superclass • can add new fields and methods • can redefine (override) a method of the superclass • must provide its own constructors, but calls superclass’s constructors • does not have direct access to its superclass’s private fields
  • 33. 3-33 public class UTurnBug extends Bug { public UTurnBug (Color bugColor) { setColor (bugColor); } ... public void turnAround () { turn(); turn(); turn(); turn(); // Or: setDirection (getDirection() + 180); } ... } A new method Constructor
  • 34. 3-34 Review: • Name a few objects used in GridWorld’s BugRunner. • Name a few library classes used in GridWorld. • What are import statements used for? • What is a field? A constructor? A method? • Which operator is used to construct an object?
  • 35. 3-35 Review (cont’d): • What is the difference between private and public methods? • Why are fields usually private? • What is inheritance? • Can a subclass add new fields? New methods?