Lesson-1-Introduction-to-Java
## OBJECT ORIENTED PROGRAMMING
### Introduction
- **Lesson 1**: Introduction to Object Oriented Programming using Java
- **Prepared by**: Marie Andrea Zurbano, IT Instructor
## TWO TYPES OF PROGRAMMING PARADIGM
### Structured Programming
- Aimed at improving clarity, quality, and development time.
- Utilizes subroutines, block structures, loops (for/while).
- Known as procedural programming; based on Top-Down Design (Edgser W. Dijkstra, 1960s).
- Control Structures:
- Sequence
- Selection/Condition
- Iteration/Loops
### Object Oriented Programming (OOP)
- Based on the concept of **objects**, which are data structures containing:
- **Data (attributes)**
- **Code (methods)**
- Programs structured from objects that interact with one another.
- Popular languages are class-based:
- Objects are instances of classes.
- Classes determine the type of objects.
## BASIC CONCEPT OF OBJECT ORIENTED PROGRAMMING
- **Objects**: Basic runtime entities representing various entities (e.g., person, account).
- Program execution involves object interactions via messages.
- Communication between objects occurs without knowledge of each other’s data or code.
- **Class**: A user-defined type, similar to a data type. An object is a variable of that type.
- Collection of similar objects. Behave like built-in data types.
## REPRESENTATION OF AN OBJECT
- Example: `PERSON`
- **Attributes**: Name, BasicPay
- **Methods**: Salary(), Tax()
## ENCAPSULATION
- Wrapping of data/methods into a single unit (class).
- Data inaccessible from outside; only methods wrapped in the class can access it.
- Enables data hiding, making objects operate like black boxes.
- One of three OOP principles (others: inheritance, polymorphism).
## ENCAPSULATION - OBJECTS AS "BLACK BOXES"
- **Information In**: Data and Method
- **Information Out**: Encapsulated processes.
## ABSTRACTION
- Represents essential features without background details.
- Classes use abstraction; defined with attributes and methods.
- Example: Mobile phone demonstrating data encapsulation and abstraction.
## INHERITANCE
- Process where one class inherits properties from another class.
- Supports hierarchical classification and promotes reusability.
- New classes can add features from existing ones without modification.
- In Java, derived classes are known as **subclasses**.
- Each subclass defines unique features.
## PROPERTY INHERITANCE EXAMPLE
- **Flying Bird**:
- Attributes: Feathers, Lay eggs
- **Nonflying Bird**: Derived from Bird class.
- Attributes: Robin, Swallow, Penguin, Kiwi
## POLYMORPHISM
- Ability to take more than one form; allows different internal structures to share external interface.
- Integral in implementing inheritance.
- Example operation (addition with different types):
- Numbers: generates sum
- Strings: concatenates.
## POLYMORPHISM EXAMPLE
- **Shape**:
- Draw()
- **Objects**:
- Triangle Object
- Box Object
- Circle Object
- Method calls: Draw(Circle), Draw(Box), Draw(Triangle)
## BENEFITS OF OOP
- Eliminates redundancy through inheritance.
- Allows building from standard modules; saves development time.
- Data hiding enhances security.
- Multiple objects can coexist without interference.
- Facilitates partitioning project work based on objects.
- Easily upgradeable from small to large systems.
- Simplifies software complexity management.
## APPLICATIONS OF OOP
- Transforming how systems are designed and implemented:
- Real-time systems
- Simulation and modeling
- Object-oriented databases
- AI and expert systems
- Neural Networks and parallel programming
- Decision support and office automation systems
- CAD Systems
## SUMMARY
- Java is an object-oriented language enabling organization of program code into objects.
- Key OOP concepts:
- Encapsulation
- Inheritance
- Polymorphism
## INTRODUCTION TO JAVA
## WHAT IS JAVA?
- Popular programming language in education and industry.
- Developed by Dr. James Gosling and colleagues at Sun Microsystems (1990s).
- Released in 1995; open-source language.
- Object-oriented features simplify software design and implementation.
## BRIEF HISTORY OF JAVA
- Initiated in June 1991 by Gosling, Sheridan, Naughton.
- Named after Java coffee.
- Java's syntax resembles C/C++ for familiarity.
- Java 1.0 released in 1995 with **"Write Once, Run Anywhere"** principle.
- Early widespread adoption in web browsers for Java applets.
- Open-sourced in 2006; acquired by Oracle in 2009.
## TYPES OF JAVA APPLICATIONS
- **Java Applets**: Stored on a website; downloaded and run on client.
- **Applications**: Need to be downloaded to run.
- **JAR (Java Archive)**: Packages Java files into one.
- **Servlets**: Run on web servers; render web pages.
- **Swing Applications**: GUI design.
- **EJB (Enterprise Java Beans)**: Develop large websites.
## CHARACTERISTICS OF JAVA
- Simple
- Object-oriented
- Distributed
- Interpreted
- Robust
- Secure
- Architecture-neutral
- Portable
- Multithreaded
- Dynamic
## BENEFITS OF JAVA
- Preferred choice for beginners and professionals.
- Method declarations ensure return values; reduces bugs.
- Extensive Java API enables code reuse.
- Platform-independent execution; compatibility across systems.
## SAMPLE JAVA PROGRAM
```java
/* Hello_World.java */
Class Hello_World{
public static void main(String[] args){
System.out.println("Hello World.");
System.out.println();
System.out.print("End of Program.");
}
}
```
- **Sample Output**:
- Hello World.
- End of Program.
## ANALYZING THE PROGRAM
- Filename: Hello_World.java; class name must match filename.
- `class`: Fundamental unit of Java program structure.
- `public static void main(String[] args)`: Entry point of execution.
- First line outputs Hello World.
## TOOLS NEEDED TO WRITE JAVA PROGRAM
- **Java SE Development Kit 8 (JDK 8)**
- **Text Editor**: Must save source code as plain text.
- Examples: Notepad, Eclipse, Netbeans, Notepad++.
## JAVA VARIABLE DECLARATION
- Variable: Value varies based on conditions.
- Declaration format: `Data Type Variable Name = Value;`
- Example: `String User_Name = "Jake R. Pomperada";`
## RULES FOR VARIABLES
### Can Contain:
- Unique characters (including digits).
- Currency sign ($).
- Connecting punctuation (_).
### Cannot Contain:
- Case-sensitive names.
- Must start with a letter.
- No whitespace or special characters allowed.
- Cannot begin with a digit.
- Must not be a reserved keyword.
## CONSTANT VARIABLES
- Value does not change during program execution.
- Useful for fixed-value assignments.
## IDENTIFIERS
- Names for program elements (e.g., variables, functions, arrays).
- Must consist of letters and digits (first character must be a letter).
- Cannot be reserved words or standard methods.
## VALID IDENTIFIERS
- Examples: Salary_Per_Month, Age, Users_Name.
## INVALID IDENTIFIERS
- Examples: 3rd (starts with a digit), while (reserved keyword).
## DATA TYPE
- Specifies the range of values, operations, and storage methods in memory.
- Common Data Types:
- `byte`: -128 to +127
- `short`: -32,768 to +32,767
- `int`: -2,147,483,648 to +2,147,483,648
- `long`: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
- `float`: +-1.401298E+45 to +-3.402823E+38
- `double`: +-4.94065645841246E-324 to +∞
- `boolean`: true/false
- `char`: UTF-16 code unit '\u0000' to '\uFFF'
## JAVA OPERATOR
- Rich set of operators categorized into:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
## ARITHMETIC OPERATORS
- Used in mathematical expressions:
- `+`, `-`, `*`, `/`, `%`
- Increment (`++`), Decrement (`--`)
## RELATIONAL OPERATORS
- Operators for comparison:
- `==`, `!=`, `>`, `