Lecture 2 B
Lecture 2 B
|
Overview
• Sub divided into two sections:
– Classes and Objects I: classes and objects
Wednesday,
September
13, 2017
OOP I | 2
Classes and Objects II
|
Objectives
• Organize classes into packages to promote
reuse.
|
Java: Syntax of a Class
Wednesday,
September
13, 2017
OOP I | 6
Java: Syntax of a Class
Wednesday,
September
13, 2017
OOP I | 7
Java: Structure of a Class
1. Package selection
2. Class declaration
Wednesday,
September
13, 2017
OOP I | 8
Working with Packages
• Packages help programmers manage complex programs
that use a lot of classes. Packages create a folder
hierarchy of the classes.
Wednesday,
September
13, 2017
OOP I | 10
Java: Structure of an Object
1. Declaration:
– Class name, object name, and initialization
2. Access (mostly public members):
– objectName.attribute
– objectName.method()
Wednesday,
September
13, 2017
OOP I | 11
Working with objects
• When an object of a class is created, its instance variables
are initialized to null by default. There are 3 ways of
initializing instance variables (from other classes):
1. Declare them as public
2. Use constructors
Wednesday,
September
13, 2017
OOP I | 12
Constructors
• Constructors are special methods used to initialize instance
variables of objects at creation. When creating an object
the new keyword is used to initiate constructor calls.
Wednesday,
September
13, 2017
OOP I | 13
Set and Get Methods
• A class’s private fields can be manipulated only
through public members of that class by a client
class (that is any class that creates the object)
Wednesday,
September
13, 2017
OOP I | 14
Java Coding
• As learned earlier, the main method begins
execution of every Java application.
Wednesday,
September
13, 2017
OOP I | 16
Wednesday,
September
13, 2017
OOP I | 17
Wednesday,
September
13, 2017
OOP I | 18
Class Work (15 Minutes)
• Explain the differences and similarities between a
constructor and a set method
Wednesday,
September
13, 2017
OOP I | 19
Data Encapsulation, Abstraction and
Hiding
|
Data Encapsulation
• Encapsulation is a technique that enables
attributes and behaviors to be wrapped (tied)
together into an object.
Wednesday,
September
13, 2017
OOP I | 22
Data Hiding
• Using access modifiers (private, protected,
and public), classes can define which
attributes and methods can be accessed by
their client classes through their objects.
Wednesday,
September
13, 2017
OOP I | 23
Data Hiding - Access
Wednesday,
September
13, 2017
OOP I | 24
Class Exercise (5 Minutes)
• Indicate True/False concerning the
following statements:
1. Access modifiers are used to implement
data hiding
2. Creation of objects enables data hiding
and abstraction
3. To execute a class, one must create an
object
4. An object is stored in RAM
5. A class is stored in RAM
Wednesday,
September
13, 2017
OOP I | 25
Class Work (15 Minutes)
Declare the class below under the package name
com.car. In the showDetails() method, display the
make and doors in separate line. Create two objects,
initialize them to any value through the constructor,
display their details.
Wednesday,
September
13, 2017
OOP I | 26
Assignment
• Explain the use of keywords this and static
Wednesday,
September
13, 2017
OOP I | 27
References
Wednesday,
September
13, 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
|