Introduction to Object-Oriented Programming (OOPs)
Definition:
Programming paradigms are styles of programming that determine how solutions to problems are structured and expressed.
Types:
Imperative Programming: Focuses on how to execute tasks, involving control flow statements and state changes. Declarative Programming: Focuses on what to execute, defining logic without specifying control flow. Structured Programming: Enhances imperative programming with block hierarchies for better manageability. Event-Driven Programming: Flow is determined by events like user actions or messages. Concurrent Programming: Allows simultaneous execution of processes to improve performance. Imperative Programming:
Involves sequences of commands. Key elements: Variables, control structures (loops, conditionals), procedures/functions. Functional Programming:
Treats computation as the evaluation of mathematical functions. Key concepts: Pure functions, immutability, first-class functions. Object-Oriented Programming (OOP):
Organizes software design around data (objects) rather than logic. History: Evolved from Simula (1960s) to Java (1990s). Key concepts: Classes and objects, encapsulation, inheritance, polymorphism, abstraction. Impact of OOP:
Software Design: Promotes modularity and maintainability. Development Efficiency: Enhances code reuse and scalability. Industry Adoption: Widely used in web development, enterprise applications, and education. Classes and Objects
Components:
Class: Blueprint for creating objects, defining attributes and methods. Object: Instance of a class with specific values. Example:
public class Car {
String color;
String brand;
int speed;
public void drive() {
System.out.println("The car is driving at " + speed + " km/h.");
}
public void stop() {
System.out.println("The car has stopped.");
}
}
Class Definition: Specifies attributes and methods. Object Creation: Instances of the class with specific values. Constructors in Java
Definition:
Constructors initialize objects. They have no return type and set the initial state of an object.
Types of Constructors:
Default Constructor: Automatically provided if no other constructors are defined. No-Argument Constructor: Explicitly defined without parameters. Parameterized Constructor: Takes arguments to initialize specific values. All-Argument Constructor: Initializes all attributes with given values. Copy Constructor: Creates a new object as a copy of an existing object. Example:
class MyClass {
int x;
String y;
// No-argument constructor
MyClass() {
this.x = 10;
this.y = "No-Arg";
}
// Parameterized constructor
MyClass(int x) {
this.x = x;
this.y = "Parameterized";
}
// Copy constructor
MyClass(MyClass other) {
this.x = other.x;
this.y = other.y;
}
}
Constructor Overloading in Java
Definition:
Allows a class to have multiple constructors with different parameter lists for flexible object initialization.
Example:
class MyClass {
int x;
String y;
MyClass() {
this.x = 0;
this.y = "Default";
}
MyClass(int x) {
this.x = x;
this.y = "Default";
}
MyClass(int x, String y) {
this.x = x;
this.y = y;
}
}
Auto-boxing and Unboxing in Java
Definition:
Auto-boxing: Automatic conversion of primitive types to their corresponding wrapper classes. Unboxing: Automatic conversion of wrapper classes back to primitive types. Example:
Integer i = 10; // Auto-boxing
int a = i; // Unboxing
Static and Final Keywords
Static Keyword:
Defines class-level variables and methods shared across all instances.
Example:
public class StaticExample {
static int counter = 0;
public StaticExample() {
counter++;
}
public static void displayCounter() {
System.out.println("Counter: " + counter);
}
}
Final Keyword:
Defines constants, prevents method overriding, and inheritance.
Example:
public class FinalExample {
final int MAX_VALUE = 100;
}
Inner Classes in Java
Definition:
Classes defined within other classes to increase encapsulation.
Types:
Member Inner Class: Non-static, can access all members of the enclosing class. Static Nested Class: Static, can only access static members of the enclosing class. Example:
class Outer {
private int outerField = 10;
class Inner {
void display() {
System.out.println("Outer field value: " + outerField);
}
}
}
Packages and ClassPath in Java
Packages:
Organize classes and interfaces into namespaces.
Example:
package com.example.util;
public class Utility {
public static void printMessage(String message) {
System.out.println(message);
}
}
ClassPath:
Specifies the location of user-defined classes and packages.
Example:
javac -classpath .;lib/* Main.java
java -classpath .;lib/* Main
Java Archive (JAR) in Java
Definition:
A JAR file is a package file format used to aggregate Java classes and resources into one file for distribution.
Benefits:
Example:
jar cf myarchive.jar -C classes
Understanding these Java concepts helps in building efficient, modular, and maintainable software systems. For more detailed information, refer to the Notion link: