Study Material
Study Material
object
Class
inhertance
polymorphism
Encapsulation
abstraction
properties, Actions
properties, Actions
// Class definition
class Car {
// Attributes
this.brand = brand;
this.model = model;
// Method
car1.drive();
car2.drive();
}
Inheritance:
// Parent class
System.out.println("Animal is eating");
System.out.println("Dog is barking");
Polymorphism:
Poly - Many
// Parent class
System.out.println("Drawing shape");
@Override
System.out.println("Drawing circle");
}
public class Rectangle extends Shape {
@Override
System.out.println("Drawing rectangle");
// Polymorphic behavior
Encapsulation:
Data Binding:
return name;
this.name = name;
return age;
this.age = age;
} else {
System.out.println("Invalid age");
}
}
student.setName("John");
student.setAge(25);
Abstraction
Data Hiding
// Constructor
this.radius = radius;
@Override
double calculateArea() {
}
// Concrete subclass representing a Rectangle
// Constructor
this.length = length;
this.width = width;
@Override
double calculateArea() {
Java Syntax: Java syntax refers to the rules and conventions for writing Java code.
Data Types: Java supports various data types such as int, double, boolean, char, etc., which
specify the type of data that a variable can hold.
Control Structures: Control structures in Java include loops (for, while, do-while) and
conditionals (if-else, switch-case) that allow you to control the flow of execution in your
program.
Exception Handling: Exception handling in Java allows you to gracefully handle runtime
errors or exceptional situations that may occur during program execution using try-catch
blocks.
int number2 = 5;
// Arithmetic operations
} else {
System.out.println(i);
try {
3.Collections Framework
List: A List is an ordered collection that allows duplicate elements. Elements in a List are
accessed by their index.
import java.util.ArrayList;
import java.util.List;
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println("All elements:");
System.out.println(name);
Set: A Set is a collection that does not allow duplicate elements. It does not guarantee the
order of elements.
import java.util.HashSet;
import java.util.Set;
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println("All elements:");
for (Integer number : numbers) {
System.out.println(number);
Map: A Map is a collection of key-value pairs where each key is unique. It does not allow
duplicate keys.
import java.util.HashMap;
import java.util.Map;
ages.put("Alice", 30);
ages.put("Bob", 25);
ages.put("Charlie", 35);
System.out.println("All entries:");
for (Map.Entry<String, Integer> entry : ages.entrySet()) {
Queue: A Queue is a data structure that follows the First-In-First-Out (FIFO) principle, where
elements are inserted at the rear and removed from the front.
import java.util.LinkedList;
import java.util.Queue;
queue.offer(10);
queue.offer(20);
queue.offer(30);
4.Concurrency
Threads: Threads are the smallest unit of execution within a process. In Java, you can create
and manage threads using the Thread class or by implementing the Runnable interface.
try {
} catch (InterruptedException e) {
e.printStackTrace();
});
synchronized (SynchronizationExample.class) {
counter++;
}
}
});
synchronized (SynchronizationExample.class) {
counter++;
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread Safety: Thread safety ensures that a piece of code or data can be safely accessed and
manipulated by multiple threads without causing data corruption or unexpected behavior.
Techniques such as synchronization, atomic operations, and immutable objects are used to
achieve thread safety.
5.Java Annotations
Spring Boot extensively uses annotations for configuration and dependency injection.
@Autowired: Used to automatically wire beans (dependencies) by type. It injects the bean
instance into a field, setter method, or constructor.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Autowired
this.myRepository = myRepository;
@Component
// Class implementation
@Service: Annotates a class to indicate that it's a service component in the business layer.
import org.springframework.stereotype.Service;
@Service
// Class implementation
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@GetMapping("/hello")
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@GetMapping("/hello")
Here's a brief overview of Dependency Injection along with example programs demonstrating
constructor injection and setter injection:
Constructor Injection: Dependencies are injected into a class through its constructor. This is
the preferred method as it ensures that all required dependencies are provided at object
creation time.
// Constructor Injection
this.myRepository = myRepository;
Setter Injection: Dependencies are injected into a class through setter methods. Setter
injection allows for optional dependencies or changing dependencies at runtime.
// Setter Injection
this.myRepository = myRepository;
In Spring Boot, dependency injection is facilitated by the Spring IoC container. Spring Boot
automatically scans for components annotated with @Component, @Service, @Repository,
etc., and manages their dependencies. When a bean is created, Spring injects any required
dependencies based on the constructor or setter methods.
The benefits of Dependency Injection include improved modularity, testability, and
maintainability of code. By decoupling classes from their dependencies, DI makes it easier to
replace or update components without affecting other parts of the system.
Understanding Dependency Injection and its principles is crucial for effectively developing
Spring Boot applications and leveraging the benefits of the Spring framework.
Inversion of Control (IoC) is a design principle where the control of object creation and
lifecycle management is inverted from the application code to an external framework or
container. In IoC, the framework controls the flow of the program and manages the
dependencies between objects. Dependency Injection (DI) is a specific implementation of
IoC, where dependencies are injected into a class rather than being created by the class itself.
DI patterns provide guidelines and best practices for implementing DI effectively.
Dependency Injection frameworks provide tools and mechanisms to facilitate the
implementation of DI in software applications.
Here's an explanation of IoC, DI patterns, and dependency injection frameworks along with
example programs:
In IoC, the control flow of an application is shifted from the application code to a central
framework or container. The framework manages object creation, configuration, and lifecycle,
allowing developers to focus on implementing business logic rather than managing
dependencies.
In Spring Boot, the IoC container manages the creation and dependency injection of beans.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(MyApplication.class, args);
DI patterns provide guidelines and best practices for implementing Dependency Injection
effectively. Some common DI patterns include Constructor Injection, Setter Injection, and
Interface Injection.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Autowired
this.myRepository = myRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Autowired
this.myRepository = myRepository;
7.Java EE Basics
Java EE (Enterprise Edition) provides a set of specifications, APIs, and runtime environments
for developing enterprise-scale applications in Java. While not mandatory for using Spring
Boot, having a basic understanding of Java EE concepts can be helpful, especially when
dealing with web applications and database connectivity. Here's a brief overview of Java EE
basics along with example programs:
Servlets:
Servlets are Java classes that handle HTTP requests and generate HTTP responses. They are
the foundation of Java web development and are used to create dynamic web pages, handle
form submissions, and perform server-side processing.
Example program demonstrating a simple servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
response.getWriter().println("Hello, World!");
JSP is a technology that allows developers to embed Java code in HTML pages to create
dynamic web content. JSP files are compiled into servlets by the server at runtime.
<!DOCTYPE html>
<html>
<head>
<title>Hello JSP</title>
</head>
<body>
</body>
</html>
JDBC is an API for connecting Java applications to relational databases and executing SQL
queries. It provides a set of classes and interfaces for database access and manipulation.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
while (resultSet.next()) {
} catch (SQLException e) {
e.printStackTrace();
Lambda Expressions:
Lambda expressions are anonymous functions that allow you to treat functionality as a
method argument or to create concise instances of single-method interfaces (functional
interfaces).
interface MyInterface {
myInterface.myMethod("World");
Streams:
Streams provide a powerful way to process collections of objects in a functional style. They
allow you to perform aggregate operations on collections (such as filter, map, reduce) in a
concise and declarative manner.
Example program demonstrating streams:
import java.util.Arrays;
import java.util.List;
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println);