Nothing Special   »   [go: up one dir, main page]

OOP in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

OOP

Final Keyword
public class FinalExample {
public static void main(String[] args) {
final int constantValue = 42;
System.out.println("The constant value is: " + constantValue);
constantValue = 24;
}
}

Reflection
public class Main {
public static void main(String[] args){
System.out.println("String class info: " + String.class);
}
}

Collection
import java.util.ArrayList;
import java.util.List;

public class Main {


public static void main(String[] args) {
List<String> arr = new ArrayList();

arr.add("Java");
arr.add("Python");

System.out.println(arr.get(0));
System.out.println(arr.get(1));
}
}
The Collection in Java is a framework that provides an architecture to store and manipulate the group of
objects. Java Collections can achieve all the operations that you perform on a data such as searching,
sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Collection
framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList).

Multi-Threading
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(" Value " + i);
}
}
}

public class Main {


public static void main(String[] args) {
new MyThread().start();
}
}
Multithreading in Java is a process of executing multiple threads simultaneously. A thread is a lightweight
sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve
multitasking.
However, we use multithreading than multiprocessing because threads use a shared memory area. They do
not allocate separate memory areas so save memory, and context-switching between the threads takes less
time than process.
Mostly used in animations and games

RMI
RMI is a Java API that allows methods of objects in one Java Virtual Machine (JVM) to be invoked from
another JVM, making it possible to create distributed applications.
 Stub (Client Side):
1. It is like the client's representative.
2. It takes requests, sends them to the remote object, and returns the results.

 Skeleton (Server Side):


1. It is like the server's representative.
2. It receives requests, processes them using the actual object, and sends back the results.
RMI allows objects in one JVM to talk to objects in another JVM.
Stub and skeleton function as messengers, handling communication between client and server.

RMI Server Side Code


import java.rmi.*;
import java.rmi.registry.*;
public class SearchServer
{
public static void main(String[] args)
{
try
{
Search obj = new SearchQuery();
LocateRegistry.createRegistry(1900);
Naming.rebind("rmi://localhost:1900"+ "/geeksforgeeks",obj);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

import java.rmi.*;
import java.rmi.server.*;
public class SearchQuery extends UnicastRemoteObject implements Search{
SearchQuery() throws RemoteException
{
super();
}
public String query(String search) throws RemoteException {
String result;
if (search.equals("Reflection in Java"))
result = "Found";
else
result = "Not Found";
return result;
}
}

import java.rmi.*;
public interface Search extends Remote
{
String query(String search) throws RemoteException;
}

RMI Client Side Code

public class Main{


public static void main(String args[]){
String answer,value="Reflection in Java";
try{
Search access = (Search)Naming.lookup("rmi://localhost:1900"+
"/geeksforgeeks");
answer = access.query(value);
System.out.println("Article on " + value + " " + answer+" at
GeeksforGeeks");
}
catch(Exception e){
System.out.println(e);
}
}
}

Web Api’s

In Java, a Web API (Application Programming Interface) typically refers to a set of protocols and tools for
building web applications. Web APIs enable different software systems to communicate with each other
over the web. They allow developers to create, access, and integrate various functionalities and services in
their applications.

 Servlets: Java Servlets are the foundation of many Java-based web applications. They manage HTTP
requests and responses, making them suitable for building the backend of a web API.
 JAX-RS (Java API for RESTful Web Services): JAX-RS is a set of APIs and annotations for building
RESTful web services in Java. It simplifies the development of RESTful APIs by providing a standard
way to map Java methods to HTTP methods.
 Jersey: Jersey is a popular implementation of the JAX-RS specification. It provides a framework for
building RESTful web services in Java.
Instance Initializer
public class Dog {
int instanceVariable;

{
System.out.println("Instance initializer block is executed");
instanceVariable = 10;

public Dog() {
System.out.println("Constructor is executed");
}

public void display() {


System.out.println("Instance Variable: " + instanceVariable);
}

public static void main(String[] args) {


Dog example = new Dog();
example.display();
}
}

You might also like