Chapter 1: Introduction to Object-Oriented Programming
1.1 Computer Systems and Programs
A Computer System consists of two essential components:
- Hardware: The physical devices that make up a computer (e.g., CPU, motherboard, hard disk, keyboard, mouse).
- Software: A set of programs that run on a computer (e.g., operating systems, applications, drivers).
Computer Programs and Languages:
- A Computer Program is a sequence of instructions/steps that tells a computer how to perform a task.
- These programs are written using a Programming Language—a formal computer language with its own specific syntax and rules.
- Once a programmer learns the rules and syntax, they write source code in a text editor or an Integrated Development Environment (IDE). The choice of language depends on the application's maintainability, scalability, performance, and security needs.
1.2 Classifications and Paradigms of Programming Languages
Programming languages can be classified into three main categories:
- Generation: Based on the level of abstraction from machine code (e.g., Machine code, assembly, high-level languages).
- Purpose: Based on the task the language is designed for (e.g., General-purpose, special-purpose).
- Paradigm: Based on the style of programming.
1.2.1 Major Programming Paradigms
A Programming Paradigm defines how code is written and structured. The most common paradigms include:
- Procedural Programming: The oldest and most basic paradigm. It uses a top-down approach where code is written as a series of procedures/functions that perform specific tasks (e.g., C, Pascal, Fortran).
- Object-Oriented Programming (OOP): Uses classes (blueprints) and objects (instances containing data and code) to structure a program. It promotes reusable and modular code (e.g., Java, Smalltalk, Ruby).
- Functional Programming: Based on self-contained functions that take inputs and produce outputs without side effects (the output does not depend on the program's state outside the function) (e.g., Haskell, Lisp, Scala).
- Logic Programming: Based on principles of logic using a declarative style. The programmer specifies the desired output rather than the step-by-step process (e.g., Prolog).
1.3 Principles of Object-Oriented Programming (OOP)
There are four fundamental pillars of OOP:
- Encapsulation: The bundling of data (variables) and methods into a single unit called an object. This hides data from the outside world and only allows access through well-defined interfaces.
- Abstraction: The process of hiding complex implementation details from the user. It allows users to focus purely on the functionality of the class without needing to understand how it works internally.
- Inheritance: The ability of a class to inherit properties and methods from another class. This allows for code reuse and the creation of complex classes from simpler ones (e.g., a "Dog" class inheriting from an "Animal" class).
- Polymorphism: The ability of a single method to behave differently depending on the specific type of object it is called upon.
1.4 Introduction to Java
- Early History: Developed by James Gosling and his team at Sun Microsystems in 1991. Initially called "Oak" (and later "Green"), it was renamed Java. Gosling designed it with a C/C++ style syntax.
- Evolution: First released in 1995. In 1999, the JDK was released under an open-source license (GPL). Over the years, Oracle (which acquired Sun) has released numerous updates introducing major features like generics (SE 5), lambda expressions (SE 8), and modularization (SE 9).
1.4.1 Key Features of Java
- Object-Oriented: Based entirely on the concept of objects.
- Platform-Independent: Java is compiled into bytecode, which can run on any device with a JVM.
- Secure: Built-in security features like bytecode verification and sandboxing.
- Robust: Designed to be reliable and fault-tolerant.
- Extensible & Open Source: Allows adding new features easily and is freely available.
1.5 Java Architecture and Execution
1.5.1 The Compilation Process
Standard high-level languages use either a Compiler (translates the whole program to machine code at once) or an Interpreter (translates line-by-line). Java uses a hybrid strategy:
- Java source code (
.java) is compiled into intermediate Java bytecodes (.class). - This bytecode acts as machine language for a theoretical computer called the JVM.
- The JVM then interprets this bytecode to run on the specific host OS (Windows, Linux, macOS).
1.5.2 JVM and JIT
- JVM (Java Virtual Machine): Software that provides the runtime environment to execute Java bytecode. It manages memory, ensures security, and guarantees platform independence.
- JIT (Just-In-Time) Compiler: A part of the runtime environment that improves performance. Because interpreting bytecode line-by-line is slow, the JIT transforms heavily used bytecodes into native machine code at runtime.
1.5.3 Java Development Kit (JDK) Hierarchy
- JDK (Java Development Kit): The full IDE suite.
JDK = JRE + Development Tools (Compilers, debuggers). - JRE (Java Runtime Environment): What you need to run a Java app.
JRE = JVM + Library Classes. - JVM: Sits inside the JRE and contains the JIT.
1.6 Java Editions
Java is divided into four main editions based on the target audience and application type:
- Java SE (Standard Edition): The foundation. Contains the core language and APIs for graphics, networking, and databases. Target: General Developers.
- Java EE (Enterprise Edition): APIs for building large, enterprise-scale applications (web dev, messaging, security). Target: Enterprise Developers.
- Java ME (Micro Edition): APIs for building lightweight applications for mobile devices and embedded systems. Target: Mobile/Embedded Developers.
- JavaFX: Newer APIs for building rich, interactive user interfaces for desktop, mobile, and web.