0% found this document useful (0 votes)
46 views

Lecture 2 - Classes Objects and Methods

Object oriented programming uses classes and objects to model real world items. A class defines the data and behavior of an object through variables and methods. Variables can be instance, static, or local and determine how data is accessed. Methods encapsulate tasks and can be called on objects. Inheritance allows classes to share characteristics through parent-child relationships. Polymorphism allows different objects to respond to the same message differently. Encapsulation hides implementation details and controls access to data.

Uploaded by

Mark Mumba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Lecture 2 - Classes Objects and Methods

Object oriented programming uses classes and objects to model real world items. A class defines the data and behavior of an object through variables and methods. Variables can be instance, static, or local and determine how data is accessed. Methods encapsulate tasks and can be called on objects. Inheritance allows classes to share characteristics through parent-child relationships. Polymorphism allows different objects to respond to the same message differently. Encapsulation hides implementation details and controls access to data.

Uploaded by

Mark Mumba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented programming

Lecture 2 - Classes, Objects and Methods


Introduction…
Objectives
• Class
• Method
• Data Types
• Basic input/output
• Objects
Variables
Photosynthesis
Variables is a container that holds data.

• It is a name of a memory location.


• Declarations reserves the memory location or area.
• There are different types of variables:
• Local Variables
• Instance Variables
• Static Variables

• 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 proximity_sensor; // instance variable

int turn_left(){

int track_sensor; // local variable

}
}
• Static Variable
• Variable declared “statically” using static key word.
• They are created when program starts and destroyed when program stops.

static int humidity_sensor; // static variable

public class robot{}


Variables…
Access Specifiers/ Modifiers
They determine how variables can be accessed and modified.
Syntax:
Variable name or identifier
Access Specifier
private int account_number;
End of statement
Data Type

• 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.

Source: (Oracle, N.d)


Data Types…
Data Types…
• There are two types:
• Primitive or basic data types
• Intrinsic/fundamental data types.

• Non-primitive data types


• Derived data types

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

public class robot{

int proximity_sensor; // instance variable

}
Method
Photosynthesis
Method is program module that contains program statements that carry out
a task.

• Encapsulates behavior.
Method Name Arguments

int area(int length, int width){

Area=length*width; // Area of a rectangle

Arithmetic Operation Task


Method…
Example
• Robot Turning Left Method

int turn_left(){

int track_sensor; // local variable

}
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.

• Two parts of an object


• Object = Data + Methods
• An object has the responsibility to know and do.
Object Oriented Programming…
Object
• An object is a computational entity that:
• Encapsulates some state
• Is able to perform actions, or methods, on this state
• Communicates with other objects via message passing

• An object represents an individual, identifiable item, unit, or entity, either


real or abstract, with a well-defined role in the problem domain.
• An "object" is anything to which a concept applies.
• Therefore, an object is a structure for incorporating data and the
procedures for working with that data.
Example:
• Printer object that is responsible for the data and methods used to interact with
your printers.
Object Oriented Programming…
Benefits
• A more intuitive transition from business analysis models to software
implementation models.
• The ability to create a great graphical user interface for the users.
• The ability to maintain and implement changes in the programs more
efficiently and rapidly.
• The ability to effectively create software systems using a team process,
allowing specialists to work on parts of the system.
• The ability to reuse code components in other programs and purchase
components written by third-party developers to increase the functionality
of programs with little effort.
• Better integration with loosely coupled distributed computing systems and
with modern operating systems.
Research
• Applications
• Implementations in other programming languages.
Review Questions
• Define:
• Object
• Variables
• Data Types
• Access Modifier
• Describe the following variable types:
• Local
• Static
• Instance
• Differentiate:
• Class and Method
• Public and Private Access Modifiers
• Discuss
• Abstraction
• Inheritance
• Polymorphism
• Encapsulation
References
• Online
• Oracle(N.d.)Retrieved from
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

• Books
• E-books
• Library

You might also like