Object-Oriented Programming (Lecture 2)
Object-Oriented Programming (Lecture 2)
programming (Lecture 2)
By: Michael SB
Outline
• 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)
Encapsulation
Inheritance
Polymorphism
Abstraction
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)
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
• 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)
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 C
• Polymorphism
• Abstraction
• SOLID principle of OOP
Readings
• Read the difference between Java Development Kit (JDK), Java Runtime
Environment (JRE), and Java Virtual machine (JVM).