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

Lecture 2 B

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)
6 views

Lecture 2 B

Uploaded by

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

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

Wednesday,
September
13, 2017
OOP I | 2
Classes and Objects II

|
Objectives
• Organize classes into packages to promote
reuse.

• Implement get and set methods.

• Initialize objects with constructors.

• Explain the use of keywords this and static.

• Describe encapsulation, data hiding and data


abstraction
Wednesday,
September
13, 2017
OOP I | 4
Creating Classes and Objects in Java

|
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

3. Inside the class (members):


1. Instance variables
2. Methods
• Constructors
• Set and get methods
• Other methods

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.

• To include a class into a package: use the keyword


package followed by the package name. Use the dot (.)
operator to create different folders.

• Classes from pre-existing libraries/packages can be


imported into any Java program using the import
keyword.
Wednesday,
September
13, 2017
OOP I | 9
Java: Syntax of an Object

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

3. Use set methods

• There are 2 ways to retrieve values of instance variables:


1. Declare them as public

2. Use get methods

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.

• When defining a constructor in a class:


– The constructor must have the same name as the class
followed by the parameter list.

– A class can have more than one constructor as long as the


parameter list vary. This is known as constructor
overloading.

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)

• Classes often provide public methods to allow


clients of the class to
– set (assign values to) private instance variables

– get (obtain values of) private instance variables

Wednesday,
September
13, 2017
OOP I | 14
Java Coding
• As learned earlier, the main method begins
execution of every Java application.

• A class that contains method main is the Java


application, otherwise it’s just a Java class. JVM
will use main as the entry point to begin execution.

• It’s common practice to create another class with


main method and inside create an object of other
classes.
Wednesday,
September
13, 2017
OOP I | 15
Class Demo

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

• Using a UML, illustrate constructor overloading using class


Account

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.

• Encapsulation aids in abstraction and data hiding


because it only allows attributes and methods to
be accessed through an object of the class.
Without encapsulation, abstraction and data
hiding is not possible.
Wednesday,
September
13, 2017
OOP I | 21
Data Abstraction
• Abstraction is a technique that is used in OOD
to hide unnecessary details in the
implementation of classes.

• Objects may know how to communicate with


each other through interfaces, but they do not
know how the other objects are implemented.

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.

• Private attributes are hidden from outside


(client) classes.

Wednesday,
September
13, 2017
OOP I | 23
Data Hiding - Access

Class A Class B Class C


inherits A
Private Accessible NOT Accessible NOT Accessible

Protected Accessible Accessible NOT Accessible

Public Accessible Accessible Accessible

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

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


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

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
|

You might also like