Java Is An Object-Oriented Language
Java Is An Object-Oriented Language
• Mehmet studies electronics, has short black hair and brown eyes.
He is 180cm and 75 kilos. Now running to class!
Our house
Hasan’s computer
Objects have state...
Lying
Red
Hooked
Broken Happy
ill
Objects have behavior….
da da
Hello, Nice to …
I am John meet you
Grrrrrrrr
Vroemm
Object Properties
❂ Identity
❂ State
❂ Behavior
on
off
myLamp
A class An object
(the concept) (the realization)
❂ Class - Category
• Properties/states data
• Functionality/Services methods
(examines/alters state)
13
Multiple Abstractions
A single thing can have multiple abstractions
Example: a protein is…
❂ a sequence of amino acids
❂ a complicated 3D shape (a fold)
❂ a surface with “pockets” for ligands
Choosing Abstractions
Abstractions can be about
❂ tangible things (a vehicle, a car, a map) or
❂ intangible things (a meeting, a route, a schedule)
An example:
❂ Abstraction name: light
❂ Light’s wattage (i.e.,energy usage)
❂ Light can be on or off
methods
Object boundary
data
Example: Pencil
location direction
penDown
Encapsulation
❂ the data belonging to an object is hidden, so variables are private
❂ methods are public
❂ we use the public methods to change or access the private data
❂ No dependence on implementation
penDown
Programming Implications
❂ Encapsulation makes programming easier
• As long as the contract is the same, the client doesn’t care about the
implementation
Car
class name
color
speed attributes
power
drive
turn right operations
turn left
stop
in Java
Car
class name
String color
int speed attributes or
int power instance variables
drive()
turnRight() methods
turnLeft()
stop()
Java Syntax
public class Car
{
// attribute declarations
private String color; Car
private int speed;
private int power;
}
Class Pencil
Pencil Name
home()
up() methods
down()
write()
Declaring objects
❂ A class can be used to create objects
❂ Objects are the instances of that class
Car
String color
int speed
int power
new
drive()
turnRight()
turnLeft()
stop()
Java's "Building Blocks"
❂ Data types
• primitive constructs (e.g., integers, floating point numbers,
characters)
❂ Class
• A description of a set of objects
• used to create objects
Primitive Data
❂ There are exactly eight primitive data types in Java
Person ayse;
Class
Person ayse;
Person
String name
String birthDate
int age
getName()
getAge
….
is of Class
ayse
Creating Objects
❂ We use the new operator to create an Class
object
Person
ayse = new Person(); String name
String birthDate
int age
Object
Declaring and Creating Objects
Class
Flower karanfil;
karanfil = new Flower(); Flower
int age
int length
int weight
getAge()
getLength()
….
is of Class
instance of
refers to
karanfil
Object
Basic approach
❂ Define class
❂ Declare objects
❂ Create objects
❂ Use objects
Using objects
• The way you work with objects is to send them messages
• Most statements using objects have the following
structure
object.method
– for example: thisPerson.setAge(24);
• This means
– the object whose name is thisPerson
– is sent the message setAge()
– along with the "value" 24
• The effect of this is to set the person's age to be 24 years
old
Example
Class
Person ayse;
Person
ayse = new Person();
String name
String birthDate
ayse.setName(“Ayse Engin“); int age
is of Class
instance of
24
refers to Ayse Engin
ayse
Object