





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the basic concepts of object-oriented programming (oop), focusing on classes, objects, states, behaviors, and access modifiers. It covers the relationship between classes and objects, the importance of state and behaviors in objects, and the role of classes in describing objects and enforcing data bundling. The document also explains the concept of inheritance and the use of access modifiers to control access to data and methods.
Typology: Exercises
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























3-Jul-
docsity.com
In old style programming, you had:^
data, which was completely passive ^
functions, which could manipulate any data
An object contains both data and methods thatmanipulate that data^
An object is
active,
not passive; it
does
things
^
An object is
responsible
for its own data
^
But: it can
expose
that data to other objects
docsity.com
You could (in a game, for example) create an objectrepresenting a rabbit
It would have data:^
How hungry it is ^
How frightened it is ^
Where it is
And methods:^
eat, hide, run, dig
docsity.com
Every object belongs to (is an instance of) a class
An object may have fields, or variables^
The class describes those fields
An object may have methods^
The class describes those methods
A class is like a template, or cookie cutter^
You use the class’s constructor to make objects
docsity.com
//Can get but not change
private double salary;
// Cannot get or set
// ConstructorEmployee(String n, double s) {
name = n; salary = s; } // Methodsvoid pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
} public String getName() { return name; } // getter }
docsity.com
instance = object
field = instance variable
method = function
sending a message to an object =
calling a function
These are all
approximately
true
docsity.com
A
FileDialog
is a
Dialog
is a
Window
is a
Container
Container
Panel
ScrollPane
Window
Dialog
Frame
FileDialog
docsity.com
In C++ there may be more than one root^
but not in Java!
In C++ an object may have more than one parent(immediate superclass)^
but not in Java!
Java has a single, strict hierarchy
docsity.com
class Person {
String name;int age;void birthday () {
age = age + 1; } }
class Employee
extends Person {
double salary;void pay () { ...} }
Every
Employee
has
name
and
age
fields and
birthday
method
as well as
a
salary
field and a
pay
method.
docsity.com
int n;
does two things:
^
It declares that
n
is an integer variable
^
It allocates space to hold a value for
n
^
For a primitive, this is all that is needed
Employee secretary;
also does two things
^
It declares that
secretary
is type
Employee
^
It allocates space to hold a
reference
to an Employee
^
For an object, this is
not
all that is needed
secretary = new Employee ( );^
This allocate space to hold a
value
for the Employee
^
Until you do this, the Employee is
null
docsity.com
Inside a class, no dots are necessary
class Person { ... age = age + 1; ...}
Outside a class, you need to say which object you aretalking to
if (john.age < 75) john.birthday ();
If you don't have an object, you cannot use its fieldsor methods!
docsity.com
Inside a class, no dots are necessary, because^
you are working on
this
object
If you wish, you can make it explicit:
class Person { ... this.age = this.age + 1; ...}
this
is like an extra parameter to the method
You usually don't need to use
this
docsity.com
class Dog { ... }class Poodle extends Dog { ... }Dog myDog;Dog rover = new Dog ();Poodle yourPoodle;Poodle fifi = new Poodle ();myDog = rover;
// ok
yourPoodle = fifi;
// ok
myDog = fifi;
//ok
yourPoodle = rover;
// illegal
yourPoodle = (Poodle) rover;
//runtime check
docsity.com
class Bird extends Animal {So birds can fly. Except penguins.
void fly (String destination) {
location = destination; } } class Penguin extends Bird {
void fly (String whatever) { } }
docsity.com