Object Oriented Programming and Java: Here (These Slide Don't Make Sense Without The Code!)
Object Oriented Programming and Java: Here (These Slide Don't Make Sense Without The Code!)
and Java
• Collection of:
– Fields (object state, data members,
instance variables, ..)
– Methods (behaviors, …)
• Each object has it's own memory for
maintaining state (the fields).
• All objects of the same type share code.
• Information Hiding.
• Don't need to know how some
component is implemented to use it.
• Implementation can change without
effecting any calling code.
• "protects us from ourselves"
class classname {
field declarations
{ initialization code }
Constructors
Methods
}
Creating an Object
Multiple Constructors
• Nope!
• There is a finalize() method that is
called when an object is destroyed.
– you don't have control over when the
object is destroyed (it might never be
destroyed).
– The JVM garbage collector takes care of
destroying objects automatically (you have
limited control over this process).
Java Programming: OOP 22
Class modifiers
• public: anyone can create an object of
the defined class.
– only one public class per file, must have
same name as the file (this is how Java
finds it!).
• default is non-public (if you don't specify
"public").
static fields
final fields
Composition
• One class has instance variables that refer to
object of another class.
• Sometimes we have a collection of objects,
the class just provides the glue.
– establishes the relationship between
objects.
• There is nothing special happening here (as
far as the compiler is concerned).
• Two kinds:
– implementation: the code that defines
methods.
– interface: the method prototypes only.
Implementation Inheritance
interface implementation