Lecture 2 A
Lecture 2 A
|
Overview
• Sub divided into two sections:
– Classes and Objects I: classes and objects
Tuesday,
September
5, 2017
OOP I | 2
Overview
• Classes and Objects I: classes and objects
Tuesday,
September
5, 2017
OOP I | 3
Classes and Objects I
|
Objectives
• Describe classes, objects, class attributes and methods.
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.
– methods
Tuesday,
September
5, 2017
OOP I | 8
Object
• An object is an instance of a class.
Tuesday,
September
5, 2017
OOP I | 10
Methods
• Methods are functions defined inside classes.
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
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
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.
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.
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
Tuesday,
September
5, 2017
OOP I | 25
Program Execution
• When you write a Java program, you design and
construct a set of classes.
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
• 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
|