Upload
paulreeves
View
217
Download
0
Embed Size (px)
Citation preview
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 1/31
Classes and Objects
Prepared by: Prof. Irysh Paulo R. Tipay, MSCS
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 2/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 3/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 4/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 5/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 6/31
Quote for the Day
“ Any fool can write code that a computer canunderstand. Good programmers write codethat humans can understand. ” - Martin Fowler
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 7/31
Recap!
What you already know… Classes are the fundamental building blocks
of a Jaa program
From Classes! we can create Objects"
#n Jaa! Objects are basically referencevariables
A Class can hae its own Method/s $eg. %he&tring 'lass hae the methods length$(!
charAt$(! inde)*f$( etc".(
%here are a lot of built-in 'lasses in Jaa $eg.&tring 'lass! &canner 'lass! etc(
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 8/31
User-Defined Classes
A class that is created by a programmer.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 9/31
Objects
Objects in Object-Oriented Programming!represents something in the real world
+g. 'ats! ,ogs! %rees! &chools! een umaneings are considered *b/ects
#n OOP, +erything under the sun isconsidered as an Object.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 10/31
Objects
*J+'%&
A%%0#1%+& +A2#*0&
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 11/31
Objects
Gala)y 3ote
A%%0#1%+&'olor 4 red
5hone number 4678988
rand 4 &amsung3etwork &ubscriber -
&mart
+A2#*0&&end %e)t Message
'all5lay games
&urf 3et
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 12/31
Objects – Try this one….
,og
A%%0#1%+& : +A2#*0& :
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 13/31
Objects – Try this one….
&tudent
A%%0#1%+& : +A2#*0& :
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 14/31
Classes
Blue-print / model that is used to create anObject.
Just like an *b/ect! the 'lass is also consist ofa set of attributes and behaiours.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 15/31
Classes
5erson
3ameAge
Address
+at&leepreath5lay
;atch %2study
5erson ob/ect 8
Joe<8
=ue>on 'ity
+at&leep
reath5lay
;atch %2study
5erson ob/ect <
Anna87
5asay 'ity
+at&leep
reath5lay
;atch %2study
#n this e)ample! twoob/ects are created from
the 5erson class.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 16/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 17/31
Putting Concept into Action
5erson
nameage
address
eatsleep
%his is actually called a 'CA&&,#AG0AM! a kind of 1MC
$unied modelling language(used to design classes
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 18/31
Putting Concept into Action
5erson
- &tring name - &tring age
-&tring address
?oid eat$(D? oid sleep$(D
%his is a formal 'lass,iagram. #t actually containsthe data type and return
types of our attributes andbehaiours.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 19/31
Let the Coding begin….
public class 5ersonE priate &tring nameD priate &tring ageD priate &tring addressD
public oid eat$(E
&ystem.out.println$A person eats(D public oid sleep$(E &ystem.out.println$A personsleeps(D
5erson
- &tring name - &tring age
-&tring address
?oid eat$(D? oid sleep$(D
%hereHs still something missingI
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 20/31
Constructor
%his is a special Method! without a returntype! that is used to set the initial alues forour Attributes
#t has the same name as the Class name
A 'lass can have more than one 'onstructor proided that they hae di!erentparameters
"efault Constructor # A constructor without
any parameters Overloaded Constructor # A constructor
with a parameter.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 21/31
Adding Constructor to our Program
public class 5ersonE priate &tring nameD priate &tring ageD priate &tring addressD
public Person$%&
name ' ()oe*+ age ' (*+ address ' (.C*+
public oid eat$(E
&ystem.out.println$A person eats(D public oid sleep$(E &ystem.out.println$A personsleeps(D
5erson
- &tring name - &tring age
-&tring address
0Person$% ?oid eat$(D
? oid sleep$(D
3*%+ theconstructor hasthe same name
as the classname
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 22/31
Adding another constructorpublic class 5ersonE
priate &tring nameD priate &tring ageD priate &tring addressD
public Person$%& name ' ()oe*+
age ' (*+ address ' (.C*+
public Person$1tringname%& thisname ' name+ age ' (*+ address ' (.C*+
public oid eat$(E
&ystem.out.println$Aperson eats(D public oid sleep$(E &ystem.out.println$Aperson sleeps(D
Kou may notice that we hae
used the “this” keyword inour program
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 23/31
Thethisreference
%hethis
reference is used to refer to ourglobal variable+ a ariable that can beaccessed by the whole class
%he opposite of the global ariable is the local
variable. Meaning it can be only accessedwithin a method.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 24/31
Thethisreferencepublic class 5ersonE
priate &tring nameD priate &tring ageD priate &tring addressD
public Person$%& name ' ()oe*+
age ' (*+ address ' (.C*+
public Person$1tringname%& thisname ' name+ age ' (*+ address ' (.C*+
c
Global
2ariable
Cocal 2ariable
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 25/31
The private access modifierpublic class 5ersonE
priate &tring nameD priate &tring ageD priate &tring addressD
public Person$%& name ' ()oe*+
age ' (*+ address ' (.C*+
public Person$1tringname%& thisname ' name+ age ' (*+ address ' (.C*+
;e want our attributesLelds to bepriate because we donHt want it to
be accessed by other classes.
%his is because of the concept of
**5 called +ncapsulation. ;hich willbe discussed in the ne)t chapter$'oming &oonI(
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 26/31
Using our Class
y now! our class is ready for useI #n order to use our class! we need to create a
new class that will make use of it.
;e can create ne2 Objects by using
3nstantiation 3nstantiation is the process of creating
ob/ects.
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 27/31
Using our Classpublic class %estE
public static oid main$&tringargsN(E 5erson p8 O new 5erson$(D 5erson p< O new5erson$Juan(D
p8.eat$(D p<.sleep$(D
c#nstantiationI
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 28/31
What happens behind the Scenes
5erson
p8 p<
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 29/31
5erson p8 O new 5erson$(D
p8 86868
86868
3ame JoeAge <8
Address ='
eat$(&leep$(
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 30/31
8/18/2019 Lecture 1 Classes and Objects
http://slidepdf.com/reader/full/lecture-1-classes-and-objects 31/31
%wo int s and a Scanner walk into a bar. %hey spot an attractie
String on her own.
%he rst int walks up to her and say “ey baby! my VM oryours:”. &he slaps him and he walks back de/ected.
%he second int walks oer. “ey cute-stuP! can we go haesome Java coPee at my place:” After a Quick slapping! he toowalks back.
%hen the Scanner ambles oer casually “;here those two
primitie types bothering you:”! he remarks.
“Kes! #Hm so glad youHre here”! she says. “%hey /ust had noClassI”