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

Object-Oriented Programming (Lecture 2)

Uploaded by

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

Object-Oriented Programming (Lecture 2)

Uploaded by

ffff
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Object-oriented

programming (Lecture 2)
By: Michael SB
Outline

• Object-oriented programming principles


• More on object-oriented design principles
Anatomy of a class

• Attributes
• Constructors
• Accessors and modifiers
• Public interface methods
• Private implementation methods
Objects (Creation and destruction)

• When we run a Java program, the JVM (Java virtual machine) allocates two types of
memory that will be used by the program to store and retrieve values:
• Stack memory (different concept from the data structure)
• Heap memory (different concept from the data structure)
Objects (Creation and Destruction)

• JVM allocates a memory on stack when a


method starts running. The stack stores:
• Local variables, method calls, and reference to an
object.
• When the method finish execution, the allocated
memory for the method will get cleared

• JVM allocates a memory on heap when we References:


create a new object. - https://www.javatpoint.com/stack-vs-hea
p-java
• Stores objects - https://www.geeksforgeeks.org/stack-vs-
• Larger than stack heap-memory-allocation/
• Unused objects will get cleared by Java’s Garbage - https://www.scaler.com/topics/java/hea
Collection (a reference for garbage collection is p-memory-and-stack-memory-in-java/
posted in the lecture 2 reference)
Object-oriented programming concepts

Encapsulation

Inheritance

Polymorphism

Abstraction
Encapsulation

• Encapsulation is a process of wrapping code and data together into a single


unit.

From our last discussion (Lecture 1):


• Encapsulation is the idea of:
• Bundling data and function (logic) together
• Exposing an interface whereby other objects can access and use it
• Restricting access to certain details
Encapsulation

• Encapsulation:
• Hide our data and implementation based our design logic.
• Maintainability: Separate public methods (interface) (won’t be touched because it’s used
by others) from implementation (might change)
• Give us more control over our data. We can apply logic on our data modifiers to maintain
data integrity. Here are some examples:
• enforce an object trying to instantiate the class provide a required data in the required format
• you can enforce password policies
• check sanity of an input information (i.e. email address format), acceptable file size and format, and so on
Encapsulation

• Encapsulation is achieved:
• By marking the attributes of the class as private, which makes them inaccessible for other objects to
modify their states directly.
• Hide methods using access modifiers that contain implementation that we don’t want to be
accessed by other objects.
• Provide a public method that allow access to the attributes and implementations based on pre-
defined logic.
Encapsulation (Example)

We want to implement an email and


password policy for sign up:
- The email must be in the correct
format (i.e. [email protected],e.g
[email protected])
- The password must be greater than
or equal to 8 characters

This example project is posted on google


classroom. To practice
- Add these attributes (confirm email
and confirm password)
- Add a policy where confirm email
Regular expressions (shortened as "regex") are must be same with email. Also,
special strings representing a pattern to be confirm password must be same with
matched in a search operation. password
Encapsulation (Example)

Three methods:
- Private
- setEmail(email: String):String
- setPassword(password:String):String
- Public
- signUp(email: String, password:String): void
Encapsulation (Example): setEmail
Encapsulation (Example): setPassword

Practice excercise:
- Try to enforce these additional
policies using regex:
- Password must contain at
least one uppercase letter.
- Password must contain at
least one number.
- Password must contain at
least one symbol.
Encapsulation (Example): signUp
Encapsulation (Example): Use and output
Inheritance

• Generalization principle of OOP allows us to consider general categories of objects


which have common properties.
• We generalize classes sharing a common attributes and behaviors (methods) to a
parent class and inherit the common attributes and methods from our specialized
classes.
• Why?
• Reusability
Inheritance

• Inheritance is the ability of classes to inherit attributes and methods from other
classes.
• It allows the programmer to build a relationship between a new class and an existing
class and define a new code in terms of existing code.
• In inheritance relationship there are two types of classes:
• Superclass/Parent class: The class from where a subclass inherits features is called superclass. It is
also called base class or parent class in java.
• Subclass/Child class: A class that inherits all the members (attributes and methods) from other class
is called subclass. It is also called a derived class, child class, or extended class.
Inheritance

• Attribute and method accessibility based on access modifiers. (Yes means can be
accessed and No means can’t be accessed)

Access Modifier With in With in Outside package with Outside package


class package subclass only
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes
Inheritance (Example)

Common attributes:
- number_of_wheels: int
- color: String
- brand: String
- model: String
- distance_Traveled: double
• Consider the following - speed: double
classes:
• Car Common methods:
• Motor - move() – increases the distance traveled attribute
• Bicycle - break() – stop the vehicle and returns the total
distance traveled

What do we do?
- We can create a parent class (e.g. vehcile) that
defines the common attributes and methods.
Inheritance (Example)
Inheritance (Types)

Class A Class A Class A

Class B Class B Class B Class C

Class C

Single Inheritance Multi-level Inheritance Hierarchical Inheritance


Next class

• Polymorphism
• Abstraction
• SOLID principle of OOP
Readings

• Read the difference between Java Development Kit (JDK), Java Runtime
Environment (JRE), and Java Virtual machine (JVM).

You might also like