Skip to main content

Posts

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

Inheriting Data members and Methods

 In Java, when a class inherits from another class, it inherits both the data members (fields or variables) and methods (functions or behaviors) of the parent class. This process is known as inheritance and is a fundamental concept of object-oriented programming. Inheriting Data Members: - Inherited data members include fields or variables declared in the parent class. - These data members are accessible in the subclass just like they are in the parent class. - Subclasses can use inherited data members directly or override them if necessary. Inheriting Methods: - Inherited methods include all the public and protected methods defined in the parent class. - Subclasses can use inherited methods directly, extend their functionality, or override them to provide custom implementations. - Private methods in the parent class are not inherited and are not accessible in the subclass. Example: Consider a simple example to illustrate inheritance in Java: // Parent class class Vehicle {     String

Use of Inheritance in Java

Use of Inheritance in Java: Inheritance is a fundamental concept in object-oriented programming (OOP) languages like Java. It allows a class (subclass or derived class) to inherit properties and behavior from another class (superclass or base class). This facilitates code reuse, promotes modularity, and enables the creation of hierarchical relationships between classes. Example: Let's consider a real-life analogy to understand inheritance better. Suppose we have a class hierarchy representing different vehicles: 1. Vehicle (Base Class):    - Properties: make, model, year    - Behaviors: start(), stop(), accelerate(), brake() 2. Car (Subclass of Vehicle):    - Additional Properties: numDoors, color    - Additional Behaviors: honk() 3. Truck (Subclass of Vehicle):    - Additional Properties: cargoCapacity    - Additional Behaviors: loadCargo(), unloadCargo() Code Example: // Base class representing a Vehicle class Vehicle {     String make;     String model;     int year;     void st

The @Override annotation

The `@Override` annotation is used in Java to indicate that a method in a subclass is overriding a method with the same signature in its superclass. It is not mandatory to use `@Override`, but it helps in detecting errors during compilation if the method signature does not match any method in the superclass. Here are some key points about `@Override`: 1. Purpose: It provides compile-time checking that a method is indeed overriding a method from a superclass. If there is a mismatch in the method signature (e.g., misspelling of method name, incorrect parameters), the compiler will generate an error. 2. Usage: `@Override` is placed immediately before the method declaration in the subclass that is intended to override a method in the superclass. 3. Compatibility: `@Override` annotation was introduced in Java 5. It can only be used with methods that are overriding a superclass method. If a method is not overriding a superclass method, using `@Override` will result in a compilation error.

The final keyword

The `final` keyword in Java is used to define constants, prevent method overriding, and create immutable classes. Here are the main uses of the `final` keyword: 1. Final Variables: When applied to a variable, the `final` keyword indicates that the variable's value cannot be changed once initialized. 2. Final Methods: When applied to a method, the `final` keyword indicates that the method cannot be overridden by subclasses. This is commonly used in class inheritance to enforce certain behaviors that should not be modified. 3. Final Classes: When applied to a class, the `final` keyword indicates that the class cannot be subclassed. This is often used to create immutable classes, which cannot be extended or modified. Here are some examples to illustrate the usage of the `final` keyword: Final Variables: public class FinalExample {     final int MAX_VALUE = 100; // Constant variable     public static void main(String[] args) {         FinalExample obj = new FinalExample();         //

Getter and setter methods

Getter and setter methods, also known as accessor and mutator methods, are used to retrieve and modify the values of private variables (fields) in a class. They provide controlled access to the class attributes, allowing encapsulation and maintaining data integrity. Here's an example demonstrating getter and setter methods in Java: public class Person {     private String name;     private int age;     // Getter method for the name attribute     public String getName() {         return name;     }     // Setter method for the name attribute     public void setName(String name) {         this.name = name;     }     // Getter method for the age attribute     public int getAge() {         return age;     }     // Setter method for the age attribute     public void setAge(int age) {         if (age >= 0 && age <= 120) { // Validate age             this.age = age;         } else {             System.out.println("Invalid age! Age should be between 0 and 120.");  

Java Code for Matrix Operations (Addition, Subtraction & Multiplication)

  Copy import java.util.Scanner; public class MatrixOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input matrix A System.out.println("Enter the number of rows and columns of matrix A:"); int rowsA = scanner.nextInt(); int colsA = scanner.nextInt(); int[][] matrixA = new int[rowsA][colsA]; System.out.println("Enter the elements of matrix A:"); for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsA; j++) { matrixA[i][j] = scanner.nextInt(); } } // Input matrix B System.out.println("Enter the number of rows and columns of matrix B:"); int rowsB = scanner.nextInt(); int colsB = scanner.nextInt(); int[][] matrixB = new int[rowsB][colsB]; System.out.println("Enter the elements of matrix B:");