SlideShare uma empresa Scribd logo
1 de 37
5-Jul-14
Basic Object-Oriented Concepts
2
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 that
manipulate 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
3
Concept: An object has state
 An object contains both data and methods that
manipulate that data
 The data represent the state of the object
 Data can also describe the relationships between this object
and other objects
 Example: A CheckingAccount might have
 A balance (the internal state of the account)
 An owner (some object representing a person)
4
Example: A “Rabbit” object
 You could (in a game, for example) create an object
representing a rabbit
 It would have data:
 How hungry it is
 How frightened it is
 Where it is
 And methods:
 eat, hide, run, dig
5
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
6
Concept: Classes are like Abstract Data Types
 An Abstract Data Type (ADT) bundles together:
 some data, representing an object or "thing"
 the operations on that data
 The operations defined by the ADT are the only
operations permitted on its data
 Example: a CheckingAccount, with operations
deposit, withdraw, getBalance, etc.
 Classes enforce this bundling together
 If all data values are private, a class can also enforce the
rule that its defined operations are the only ones permitted
on the data
7
Example of a class
class Employee {
// Fields
private String name; //Can get but not change
private double salary; // Cannot get or set
// Constructor
Employee(String n, double s) {
name = n; salary = s;
}
// Methods
void pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
}
public String getName() { return name; } // getter
}
8
Approximate Terminology
 instance = object
 field = instance variable
 method = function
 sending a message to an object =
calling a function
 These are all approximately true
9
Concept: Classes form a hierarchy
 Classes are arranged in a treelike structure called a
hierarchy
 The class at the root is named Object
 Every class, except Object, has a superclass
 A class may have several ancestors, up to Object
 When you define a class, you specify its superclass
 If you don’t specify a superclass, Object is assumed
 Every class may have one or more subclasses
10
Example of (part of) a hierarchy
A FileDialog is a Dialog is a Window is a Container
Container
Panel ScrollPane Window
Dialog Frame
FileDialog
11
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
12
Concept: Objects inherit from superclasses
 A class describes fields and methods
 Objects of that class have those fields and methods
 But an object also inherits:
 the fields described in the class's superclasses
 the methods described in the class's superclasses
 A class is not a complete description of its objects!
13
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.
14
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
15
Notation: How to declare and create objects
Employee secretary; // declares secretary
secretary = new Employee (); // allocates space
Employee secretary = new Employee(); // does both
 But the secretary is still "blank" (null)
secretary.name = "Adele"; // dot notation
secretary.birthday (); // sends a message
16
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 are
talking to
if (john.age < 75) john.birthday ();
 If you don't have an object, you cannot use its fields
or methods!
17
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
18
Concept: A variable can hold subclass objects
 Suppose B is a subclass of A
 A objects can be assigned to A variables
 B objects can be assigned to B variables
 B objects can be assigned to A variables, but
 A objects can not be assigned to B variables
 Every B is also an A but not every A is a B
 You can cast: bVariable = (B) aObject;
 In this case, Java does a runtime check
19
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
20
Concept: Methods can be overridden
 So birds can fly. Except penguins.
class Bird extends Animal {
void fly (String destination) {
location = destination;
}
}
class Penguin extends Bird {
void fly (String whatever) { }
}
21
Concept: Don't call functions, send messages
Bird someBird = pingu;
someBird.fly ("South America");
 Did pingu actually go anywhere?
 You sent the message fly(...) to pingu
 If pingu is a penguin, he ignored it
 Otherwise he used the method defined in Bird
 You did not directly call any method
 You cannot tell, without studying the program, which
method actually gets used
 The same statement may result in different methods being
used at different times
22
Sneaky trick: How to use overridden methods
class FamilyMember extends Person {
void birthday () { // override birthday() in Person
super.birthday (); // call overridden method
givePresent (); // and add your new stuff
}
}
23
Concept: Constructors make objects
 Every class has a constructor to make its objects
 Use the keyword new to call a constructor
secretary = new Employee ( );
 You can write your own constructors; but if you don’t,
 Java provides a default constructor with no arguments
 It sets all the fields of the new object to zero
 If this is good enough, you don’t need to write your own
 The syntax for writing constructors is almost like that for
writing methods
24
Syntax for constructors
 Do not use a return type and a name; use only the
class name
 You can supply arguments
Employee (String theName, double theSalary) {
name = theName;
salary = theSalary;
}
25
Trick: Give field and parameter the same name
 A parameter overrides a field with the same name
 But you can use this.name to refer to the field
 class Person {
String name;
int age;
Person (String name, int age) {
this.name = name;
this.age = age;
}
}
 Using the same name is a common and useful convention
26
Internal workings: Constructor chaining
 If an Employee is a Person, and a Person is an
Object, then when you say new Employee ()
 The Employee constructor calls the Person constructor
 The Person constructor calls the Object constructor
 The Object constructor creates a new Object
 The Person constructor adds its own stuff to the Object
 The Employee constructor adds its own stuff to the Person
27
The case of the vanishing constructor
 If you don't write a constructor for a class, Java provides
one (the default constructor)
 The one Java provides has no arguments
 If you write any constructor for a class, Java does not
provide a default constructor
 Adding a perfectly good constructor can break a
constructor chain
 You may need to fix the chain
28
Example: Broken constructor chain
class Person {
String name;
Person (String name) {
this.name = name;
}
}
class Employee extends Person {
double salary;
Employee ( ) {
salary = 12.50;
}
}
 cannot resolve symbol – constructor Person()
Java tries to execute
an implicit super()
at this point
super();
29
Fixing a broken constructor chain
 Special syntax: super(...) calls the superclass constructor
 When one constructor calls another, that call must be first
class Employee {
double salary;
Employee (String name) {
super(name); // must be first
salary = 12.50;
}
}
 Now you can only create Employees with names
 This is fair, because you can only create Persons with names
30
Trick: one constructor calling another
 this(...) calls another constructor for this same class
 It is poor style to have the same code more than once
 If you call this(...), that call must be the first thing in your
constructor
class Something {
Something (int x, int y, int z) {
// do a lot of work here
}
Something ( ) { this (0, 0, 0); }
}
31
Concept: You can control access
class Person {
public String name;
private String age;
protected double salary;
public void birthday { age++; }
}
 Each object is responsible for its own data
 Access control lets an object protect its data and
its methods
 Access control is the subject of a different lecture
32
Concept: Classes can have fields and methods
 Usually a class describes fields (variables) and
methods for its objects (instances)
 These are called instance variables and instance methods
 A class can have its own fields and methods
 These are called class variables and class methods
 There is exactly one copy of a class variable, not
one per object
 Use the special keyword static to say that a field
or method belongs to the class instead of to objects
33
Example of a class variable
class Person {
String name;
int age;
static int population;
Person (String name) {
this.name = name;
this.age = 0;
population++;
}
}
34
Advice: Restrict access
 Always, always strive for a narrow interface
 Follow the principle of information hiding:
 the caller should know as little as possible about how the
method does its job
 the method should know little or nothing about where or why
it is being called
 Make as much as possible private
 Your class is responsible for it’s own data; don’t
allow other classes to screw it up!
35
Advice: Use setters and getters
 This way the object maintains control
 Setters and getters have conventional names: setDataName,
getDataName, isDataName (booleans only)
class Employee extends Person {
private double salary;
private boolean male;
public void setSalary (double newSalary) {
salary = newSalary;
}
public double getSalary () { return salary; }
public boolean isMale() { return male; }
}
36
Kinds of access
 Java provides four levels of access:
 public: available everywhere
 protected: available within the package (in the same
subdirectory) and to all subclasses
 [default]: available within the package
 private: only available within the class itself
 The default is called package visibility
 In small programs this isn't important...right?
37
The End

Mais conteúdo relacionado

Mais procurados

Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan vo
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 

Mais procurados (20)

Advanced php
Advanced phpAdvanced php
Advanced php
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
Ios development
Ios developmentIos development
Ios development
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 

Destaque

Software Engineering course
Software Engineering courseSoftware Engineering course
Software Engineering courseJeremy Rose
 
المحاضرة الرابعة والخامسة
المحاضرة الرابعة والخامسةالمحاضرة الرابعة والخامسة
المحاضرة الرابعة والخامسةAhmed Alageed
 
Secondary storage devices.
Secondary storage devices.Secondary storage devices.
Secondary storage devices.umamani113
 

Destaque (6)

Software Engineering course
Software Engineering courseSoftware Engineering course
Software Engineering course
 
المحاضرة الرابعة والخامسة
المحاضرة الرابعة والخامسةالمحاضرة الرابعة والخامسة
المحاضرة الرابعة والخامسة
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
1 se-introduction
1 se-introduction1 se-introduction
1 se-introduction
 
Secondary storage devices.
Secondary storage devices.Secondary storage devices.
Secondary storage devices.
 

Semelhante a 38 object-concepts

Basic OOPs Concepts (E-next.in).pdf
Basic OOPs Concepts (E-next.in).pdfBasic OOPs Concepts (E-next.in).pdf
Basic OOPs Concepts (E-next.in).pdfShubhamMadaan9
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in javaHarish Gyanani
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptmuneshwarbisen1
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptRithwikRanjan
 

Semelhante a 38 object-concepts (20)

Basic OOPs Concepts (E-next.in).pdf
Basic OOPs Concepts (E-next.in).pdfBasic OOPs Concepts (E-next.in).pdf
Basic OOPs Concepts (E-next.in).pdf
 
Object concepts
Object conceptsObject concepts
Object concepts
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
Ch2
Ch2Ch2
Ch2
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 

Último

Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...SofiyaSharma5
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024APNIC
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxellan12
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.soniya singh
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...Diya Sharma
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Servicesexy call girls service in goa
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 

Último (20)

Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Sector 63 Mamura Noida ✔️☆9289244007✔️☆ Female E...
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 

38 object-concepts

  • 2. 2 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 that manipulate 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
  • 3. 3 Concept: An object has state  An object contains both data and methods that manipulate that data  The data represent the state of the object  Data can also describe the relationships between this object and other objects  Example: A CheckingAccount might have  A balance (the internal state of the account)  An owner (some object representing a person)
  • 4. 4 Example: A “Rabbit” object  You could (in a game, for example) create an object representing a rabbit  It would have data:  How hungry it is  How frightened it is  Where it is  And methods:  eat, hide, run, dig
  • 5. 5 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
  • 6. 6 Concept: Classes are like Abstract Data Types  An Abstract Data Type (ADT) bundles together:  some data, representing an object or "thing"  the operations on that data  The operations defined by the ADT are the only operations permitted on its data  Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.  Classes enforce this bundling together  If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data
  • 7. 7 Example of a class class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter }
  • 8. 8 Approximate Terminology  instance = object  field = instance variable  method = function  sending a message to an object = calling a function  These are all approximately true
  • 9. 9 Concept: Classes form a hierarchy  Classes are arranged in a treelike structure called a hierarchy  The class at the root is named Object  Every class, except Object, has a superclass  A class may have several ancestors, up to Object  When you define a class, you specify its superclass  If you don’t specify a superclass, Object is assumed  Every class may have one or more subclasses
  • 10. 10 Example of (part of) a hierarchy A FileDialog is a Dialog is a Window is a Container Container Panel ScrollPane Window Dialog Frame FileDialog
  • 11. 11 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
  • 12. 12 Concept: Objects inherit from superclasses  A class describes fields and methods  Objects of that class have those fields and methods  But an object also inherits:  the fields described in the class's superclasses  the methods described in the class's superclasses  A class is not a complete description of its objects!
  • 13. 13 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.
  • 14. 14 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
  • 15. 15 Notation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both  But the secretary is still "blank" (null) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message
  • 16. 16 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 are talking to if (john.age < 75) john.birthday ();  If you don't have an object, you cannot use its fields or methods!
  • 17. 17 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
  • 18. 18 Concept: A variable can hold subclass objects  Suppose B is a subclass of A  A objects can be assigned to A variables  B objects can be assigned to B variables  B objects can be assigned to A variables, but  A objects can not be assigned to B variables  Every B is also an A but not every A is a B  You can cast: bVariable = (B) aObject;  In this case, Java does a runtime check
  • 19. 19 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
  • 20. 20 Concept: Methods can be overridden  So birds can fly. Except penguins. class Bird extends Animal { void fly (String destination) { location = destination; } } class Penguin extends Bird { void fly (String whatever) { } }
  • 21. 21 Concept: Don't call functions, send messages Bird someBird = pingu; someBird.fly ("South America");  Did pingu actually go anywhere?  You sent the message fly(...) to pingu  If pingu is a penguin, he ignored it  Otherwise he used the method defined in Bird  You did not directly call any method  You cannot tell, without studying the program, which method actually gets used  The same statement may result in different methods being used at different times
  • 22. 22 Sneaky trick: How to use overridden methods class FamilyMember extends Person { void birthday () { // override birthday() in Person super.birthday (); // call overridden method givePresent (); // and add your new stuff } }
  • 23. 23 Concept: Constructors make objects  Every class has a constructor to make its objects  Use the keyword new to call a constructor secretary = new Employee ( );  You can write your own constructors; but if you don’t,  Java provides a default constructor with no arguments  It sets all the fields of the new object to zero  If this is good enough, you don’t need to write your own  The syntax for writing constructors is almost like that for writing methods
  • 24. 24 Syntax for constructors  Do not use a return type and a name; use only the class name  You can supply arguments Employee (String theName, double theSalary) { name = theName; salary = theSalary; }
  • 25. 25 Trick: Give field and parameter the same name  A parameter overrides a field with the same name  But you can use this.name to refer to the field  class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; } }  Using the same name is a common and useful convention
  • 26. 26 Internal workings: Constructor chaining  If an Employee is a Person, and a Person is an Object, then when you say new Employee ()  The Employee constructor calls the Person constructor  The Person constructor calls the Object constructor  The Object constructor creates a new Object  The Person constructor adds its own stuff to the Object  The Employee constructor adds its own stuff to the Person
  • 27. 27 The case of the vanishing constructor  If you don't write a constructor for a class, Java provides one (the default constructor)  The one Java provides has no arguments  If you write any constructor for a class, Java does not provide a default constructor  Adding a perfectly good constructor can break a constructor chain  You may need to fix the chain
  • 28. 28 Example: Broken constructor chain class Person { String name; Person (String name) { this.name = name; } } class Employee extends Person { double salary; Employee ( ) { salary = 12.50; } }  cannot resolve symbol – constructor Person() Java tries to execute an implicit super() at this point super();
  • 29. 29 Fixing a broken constructor chain  Special syntax: super(...) calls the superclass constructor  When one constructor calls another, that call must be first class Employee { double salary; Employee (String name) { super(name); // must be first salary = 12.50; } }  Now you can only create Employees with names  This is fair, because you can only create Persons with names
  • 30. 30 Trick: one constructor calling another  this(...) calls another constructor for this same class  It is poor style to have the same code more than once  If you call this(...), that call must be the first thing in your constructor class Something { Something (int x, int y, int z) { // do a lot of work here } Something ( ) { this (0, 0, 0); } }
  • 31. 31 Concept: You can control access class Person { public String name; private String age; protected double salary; public void birthday { age++; } }  Each object is responsible for its own data  Access control lets an object protect its data and its methods  Access control is the subject of a different lecture
  • 32. 32 Concept: Classes can have fields and methods  Usually a class describes fields (variables) and methods for its objects (instances)  These are called instance variables and instance methods  A class can have its own fields and methods  These are called class variables and class methods  There is exactly one copy of a class variable, not one per object  Use the special keyword static to say that a field or method belongs to the class instead of to objects
  • 33. 33 Example of a class variable class Person { String name; int age; static int population; Person (String name) { this.name = name; this.age = 0; population++; } }
  • 34. 34 Advice: Restrict access  Always, always strive for a narrow interface  Follow the principle of information hiding:  the caller should know as little as possible about how the method does its job  the method should know little or nothing about where or why it is being called  Make as much as possible private  Your class is responsible for it’s own data; don’t allow other classes to screw it up!
  • 35. 35 Advice: Use setters and getters  This way the object maintains control  Setters and getters have conventional names: setDataName, getDataName, isDataName (booleans only) class Employee extends Person { private double salary; private boolean male; public void setSalary (double newSalary) { salary = newSalary; } public double getSalary () { return salary; } public boolean isMale() { return male; } }
  • 36. 36 Kinds of access  Java provides four levels of access:  public: available everywhere  protected: available within the package (in the same subdirectory) and to all subclasses  [default]: available within the package  private: only available within the class itself  The default is called package visibility  In small programs this isn't important...right?