Lecture 2 - Classes Objects and Methods
Lecture 2 - Classes Objects and Methods
• Local Variables
• Variables declared inside a method.
Example: int turn_left(){
int track_sensor; // local variable
}
Variables…
Types of variables…
• Instance Variable
• Variable declared inside a class but outside a method.
public class robot{
int turn_left(){
}
}
• Static Variable
• Variable declared “statically” using static key word.
• They are created when program starts and destroyed when program stops.
• Default
• No modifier.
• Visible to all classes in the same Java package
Example: int account_number;
Variables…
Access Specifiers/ Modifiers…
• Private
• Cannot be changed from outside of the defined class.
• Visible only to the class in which they belong.
Example: private int account_number;
• Public
• Can be accessed and changed from outside of the class.
• Visible to all classes.
Example: public int account_number;
• Protected
• Visible to the classes to which they belong and any subclasses.
• Subclass inherits variables and methods from superclass.
Example: protected int account_number;
Data Types
Photosynthesis
Data Type is an attribute of a variable indicating how it can be used.
• Data type specifies size and values that can be stored in a variable.
Task
• Identify the primitive and non primitive data types.
Class
Photosynthesis
Class is an object blue print.
• It is declared by use of a class keyword with the body enclosed with curly
braces {}.
• Variables defined within a class are called instance variables. Because
each instance of a class(object) has their own copy of defined variables.
Class…
Example
• Robot Program
Class Name
Access Specifier
}
Method
Photosynthesis
Method is program module that contains program statements that carry out
a task.
• Encapsulates behavior.
Method Name Arguments
int turn_left(){
}
Implementation
Example
• Robot Program
Class
Main Method
Other Methods
Implementation…
Example
• Robot Program
• The main method
controlled the execution
of other methods.
Implementation…
Example… MAIN PROGRAM
• Robot Program…
Main Method
• A method can be called
many times. Method 1x3
SUBROUTINES
Method 2x20
Method 3 Method 1
Stop();
Method 2
Method 3
Object Oriented Programming(OOP)
Photosynthesis
OOP is an approach to software development in which the structure of the
software is based on objects interacting with each other to accomplish a task.
• This interaction takes the form of messages passing back and forth
between the objects.
• In response to message, an object perform an action.
Example:
• A Robotic Car object sends messages in the form of small electrical
signals to the actuator object which interacts with the microcontroller
object to do a specific task. The tasks could include:
• Move_Forward()
• Turn_Left()
• Turn_Right()
• Stop()
Objects…
• Programs are simulations of real-world behavior
• Expressed by identifying objects and asking them to perform certain
tasks.
• Focus is on object, then the task.
• A computer language is object-oriented if they support the four specific
object properties:
• Abstraction
• Polymorphism
• Inheritance
• Encapsulation.
Objects…
Abstraction
• The ability to filter out the extraneous properties of objects.
• Abstraction refers to the act of representing essential features without
including the background details or explanations.
• Not showing the implementation details.
Example:
• I may not need to know the revolutions per minute( RPM) of the engine
when driving an automatic car.
Objects…
Encapsulation
• Encapsulation is the process in which no direct access is granted to the data, instead
it is hidden.
• If you want to gain access to the data, you must interact with the object responsible
for the data.
• By encapsulating data, you make the data of your system more secure and reliable.
• The wrapping of data and functions into a single unit (called class) is known as
encapsulation.
• Is the practice of including in an object everything it needs hidden from other
objects.
• The internal state is usually not accessible by other objects.
• The data is not accessible to the outside world and only those functions in the class
can access it
• Example:
• The human resources manager encapsulates the information about employees, and determines
how this data can be used or manipulated. Any request for the employee data or request to
update the data must be routed through the manager.
Objects…
Polymorphism
• Polymorphism means ability to be more than one form.
• Polymorphism is the ability of two different objects to respond to the same
request message in their own unique way.
• An object has “multiple identities”.
• Polymorphism is extensively used in implementing inheritance.
• In OOP, you implement this type of polymorphism through a process called
overloading. You can implement different methods of an object that have the
same name.
• Example:
• You can create two methods of an inventory object to look up the price of a
product. Both of these methods would be named, getPrice. Another object could
call this method and pass either the name of the product or the product ID. The
inventory object could tell which getPrice method to run by whether a string value
or an integer value was passed with the request.
Objects…
Inheritance
• Most objects are classified in hierarchies. You use inheritance in OOP to
classify the objects in your programs according to common characteristics
and function.
• It makes programming easier, because it enables you to combine general
characteristics into a parent object and inherit these characteristics in the
child object.
• The sub-class inherits the base class’ data members and member
functions.
• In OOP, the concept of inheritance provides the idea of reusability.
• Example:
• You can classify all cats together as having certain common
characteristics, such as having four legs and fur. Their specific breeds
further be classified.
Object Oriented Programming…
Why objects?
• Modularity
• Large software projects can be split up in smaller pieces.
• Reusability
• Programs can be assembled from pre-written software components.
• Extensibility
• New software components can be written or developed from existing
ones.
• Books
• E-books
• Library