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

Lecture 2 A

Uploaded by

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

Lecture 2 A

Uploaded by

Raj Gami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Classes and Objects

|
Overview
• Sub divided into two sections:
– Classes and Objects I: classes and objects

– Classes and Objects II: creating classes and


objects in Java

Tuesday,
September
5, 2017
OOP I | 2
Overview
• Classes and Objects I: classes and objects

• Classes and Objects II: creating classes and


objects in Java

Tuesday,
September
5, 2017
OOP I | 3
Classes and Objects I

|
Objectives
• Describe classes, objects, class attributes and methods.

• Declare methods in classes to implement its behaviors.

• Declare instance variables to implement its attributes.

• Differentiate between instance variables of a class and local


variables of a method.

• Model a class using UML

• Differentiate between Access Modifiers (Public, Protected


and Private).

Tuesday,
September
5, 2017
OOP I | 5
Class Demo
• Video
– https://www.youtube.com/watch?v=vS
znJAwSd78

Tuesday,
September
5, 2017
OOP I | 6
Classes, Objects, Instance Variables,
and Methods

|
Class
• A class is a template for creating multiple objects
with similar features. Classes provide all the
features of a particular set of objects.

• Classes are composed of 2 main items:


– Instance variables

– methods

• A class library (.jar) is a set of classes.

Tuesday,
September
5, 2017
OOP I | 8
Object
• An object is an instance of a class.

• Once you define a class, you use the class to


create an object.

• Classes can be reused to create different kinds


of objects as needed by the program. The
program uses the objects and discards them
when it finishes executing.
Tuesday,
September
5, 2017
OOP I | 9
Instance Variables
• Instance variables are variables defined inside a
class. Instance variables describe the attributes of
an object.

• The class defines the kind of attribute, and each


object stores its own value for that attribute.

• Attributes are the individual values/qualites that


differentiate one object from another.

Tuesday,
September
5, 2017
OOP I | 10
Methods
• Methods are functions defined inside classes.

• A class defines a method (or provides the


lines of code for a method), and an object
calls (or executes) the lines of code.

• Behavior can be described as the tasks that


an object can execute.

Tuesday,
September
5, 2017
OOP I | 11
Class Exercise (5 Minutes)
• Indicate True/False
1. A class is composed of attributes and
methods and objects
2. Variables can exist outside a class
3. Variables can exist outside a method
4. One class can be used to create 10
objects
5. One object can be created from 10
classes

Tuesday,
September
5, 2017
OOP I | 12
Unified Modelling Language

|
UML Notation
• Usually, programmers model classes using Unified Modelling
Language (UML) so as to enhance communication.

Tuesday,
September
5, 2017
OOP I | 14
Class Demo
• Design UML of a class called Motorcycle with the
following characteristics:

Class Motorcycle
Attributes: make, color, engineState

Behaviors: showAttributes(), startEngine(),


stopEngine()

Events: onStartEngine(), onStopEngine()

Tuesday,
September
5, 2017
OOP I | 15
UML Class Diagrams

Tuesday,
September
5, 2017
OOP I | 16
Class Work (15 Minutes)
• Design UML of a class called Account with the following
characteristics:
Class Account
Attributes: accountNumber, balance

Behaviors: showAttributes(), getBalance(),


setBalance()

Events:

Tuesday,
September
5, 2017
OOP I | 17
Class Exercise
Write a Java code that will declare the class
illustrated below.

Tuesday,
September
5, 2017
OOP I | 18
Access Modifiers

|
Access Modifiers
• private, public and protected are called access
modifiers. Declaring instance variables or methods
with access modifiers private is know as Data
Hiding. They are only accessible within the same
class.

• Variables and methods declared with public access


modifier are accessible in methods outside that
class in which they are declared.
Tuesday,
September
5, 2017
OOP I | 20
Class Exercise (5 Minutes)
• When is it ideal to declare an attribute as
public?
• When is it ideal to declare a method as
private?
• Can set and get methods be private?
• When creating classes, are the classes
declared as private or public?

Tuesday,
September
5, 2017
OOP I | 21
Object Creation

|
Object Creation
• Typically, you cannot initialize an attribute or
call (or execute) a method that belongs to
class until you create an object of that class.

• Just like variables, objects also have to be


initialized before being used.
int a = 10;
Motorcycle cycle = new Motorcycle();

Tuesday,
September
5, 2017
OOP I | 23
Attribute & Method Calls
• The object contains all the attributes and methods of
the class. However, Only public members of the class
can be accessed from an object

• The dot separator (.) is used immediately after the


object name, in order to access an attribute or make a
method call

• The arguments passed in the object method must match


the type and count of the parameters declared in the
class method.
Tuesday,
September
5, 2017
OOP I | 24
Class Demo

Motorcycle cycle = new Motorcycle();


cycle.wheels = 2;
cycle.startEngine();

Tuesday,
September
5, 2017
OOP I | 25
Program Execution
• When you write a Java program, you design and
construct a set of classes.

• Then, when your program runs, objects (or


instances) of those classes are created and
discarded as needed.

• Your main task, as a Java programmer, is to create


the right set of classes to accomplish what your
program needs to accomplish.
Tuesday,
September
5, 2017
OOP I | 26
Class Work (15 Minutes)
• Create at least 2 objects of the class
Account.
– Initialize all the attributes of the objects
– Call at least one method for each object

Tuesday,
September
5, 2017
OOP I | 27
Assignment
• Explain the difference between a local variable
and an instance variable

Tuesday,
September
5, 2017
OOP I | 28
References

• P.J. Deitel and H.M. Deitel (2007), Java


How to Program. 7th Edition. Pearson
Education Inc.

• https://www.tutorialspoint.com/uml

Tuesday,
September
5, 2017
OOP I | 29
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|

You might also like