Lecture 4 Object Oriented Programming
Lecture 4 Object Oriented Programming
Programming Concepts
Objectives
• This lesson will introduce you to:
• Objects
• Classes
• Inheritance
• Interfaces
• Packages
Objects
• Objects are key to understanding object-oriented technology.
• An object is a software bundle of related state and behavior
• Software objects are often used to model the real-world objects that
you find in everyday life.
• Real-world objects share two characteristics
• have state and behavior
• Examples of objects:
• Dogs:
• state (name, color, breed, hungry)
• behavior (barking, fetching, wagging tail).
Objects
• Examples of Objects:
• Bicycles:
• State (current gear, current pedal cadence, current speed)
• Behavior (changing gear, changing pedal cadence, applying brakes).
Software objects
• A software object:
• stores its state in fields (variables)
• exposes its behavior
through methods (functions in some
programming languages).
• Methods:
• operate on an object's internal state
• serve as the primary mechanism for object-
to-object communication
Software objects
• Hiding internal state and requiring all
interaction to be performed through an
object's methods is known as data
encapsulation
• Encapsulation is a fundamental principle of
object-oriented programming.
• Encapsulation allows an object to be in
control of how the outside world is
allowed to use it
A bicycle modeled as a software object
Benefits of software objects in programming
• Modularity:
• The source code for an object can be written and maintained independently of the source
code for other objects.
• Once created, an object can be easily passed around inside the system.
• Information-hiding:
• By interacting only with an object's methods, the details of its internal implementation
remain hidden from the outside world.
• Code re-use:
• If an object already exists you can use that object in your program
• allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to
run in your own code.
• Pluggability and debugging ease:
• If a particular object turns out to be problematic, you can simply remove it from your
application and plug in a different object as its replacement.
Classes
• A class is the blueprint from which individual objects are created
• An object is an instance of a particular class
class MyClass {
// field, constructor, and
// method declarations
}
Classes
Classes
In general, class declarations can include these components, in order:
• Modifiers such as public, private, and a number of others that you will
encounter later.
• The class name, with the initial letter capitalized by convention.
• The name of the class's parent (superclass), if any, preceded by the
keyword extends. A class can only extend (subclass) one parent.
• A comma-separated list of interfaces implemented by the class, if any,
preceded by the keyword implements.
• A class can implement more than one interface.
• The class body, surrounded by braces, {}.
Classes: Variable types
A class can contain any of the following variable types:
• Local variables:
• defined inside methods, constructors or blocks are called local variables
• destroyed when the method has completed.
• Instance variables:
• variables within a class but outside any method
• initialized when the class is instantiated
• can be accessed from inside any method, constructor or blocks of that particular
class.
• Class variables:
• declared within a class, outside any method, with the static keyword.
• Parameters:
• Variables in method declarations
All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.
All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.
Things to note
• All variables must have a type.
• You can use primitive types such as int, float, boolean, etc.
• Or you can use reference types, such as strings, arrays, or objects.
• the first letter of a class name should be capitalized
• the first (or only) word in a method name should be a verb.
Access Modifiers