Object-Oriented Programming Concepts: Classes, Objects, and Access Modifiers, Exercises of Java Programming

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

2011/2012

Uploaded on 07/19/2012

olala
olala 🇮🇳

4.5

(2)

34 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3-Jul-12
Basic Object-Oriented Concepts
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Object-Oriented Programming Concepts: Classes, Objects, and Access Modifiers and more Exercises Java Programming in PDF only on Docsity!

3-Jul-

Basic Object-Oriented Concepts

docsity.com

Concept: An object has behaviors

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

Example: A “Rabbit” object

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

Concept: Classes describe objects

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

Example of a class class Employee {// Fieldsprivate String name;

//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

Approximate Terminology

instance = object 

field = instance variable 

method = function 

sending a message to an object =

calling a function

These are all

approximately

true

docsity.com

Example of (part of) a hierarchy

A

FileDialog

is a

Dialog

is a

Window

is a

Container

Container

Panel

ScrollPane

Window

Dialog

Frame

FileDialog

docsity.com

C++ is different

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

Example of inheritance

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

Concept: Objects must be created

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

Notation: How to reference a field or method

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

Concept:

this

object

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

Example: Assignment of subclasses

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

Concept: Methods can be overridden

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