Networking[1]
Networking[1]
1. **Variables:** Containers for storing data values. Types include ‘int‘, ‘float‘, ‘char‘, ‘boolean‘, etc.
2. **Data Types:** Defines the type of data that can be stored and manipulated within a program. Includes
primitive types (like ‘int‘, ‘float‘, etc.) and reference types (like arrays, classes).
3. **Operators:** Symbols that perform operations on variables and values (e.g., ‘+‘, ‘-‘, ‘*‘, ‘/‘).
4. **Control Flow Statements:** Includes ‘if-else‘, ‘switch‘, loops (‘for‘, ‘while‘, ‘do-while‘), and branching
statements (‘break‘, ‘continue‘, ‘return‘).
5. **Arrays:** Containers that hold a fixed number of values of a single type.
An abstract class is a class that cannot be instantiated and is often used as a base class. It can contain
abstract methods (without a body) that must be implemented by subclasses, as well as regular methods.
‘‘‘java
abstract class Animal {
abstract void makeSound(); // Abstract method
public void sleep() {
System.out.println("Sleeping...");
}
}
### Interfaces
An interface is a reference type in Java that is similar to a class. It is a collection of abstract methods. A
class implements an interface, thereby inheriting the abstract methods of the interface.
‘‘‘java
interface Animal {
void makeSound(); // Abstract method
}
Methods in Java are defined inside a class and are used to perform certain actions. A method definition
includes a method signature (name, parameters) and a body.
‘‘‘java
class Example {
void printMessage() {
System.out.println("Hello, World!");
}
}
‘‘‘
### Inheritance
Inheritance is a mechanism where one class acquires the properties and behaviors (methods) of another
class. The class that inherits is called the subclass (or derived class), and the class being inherited from is
called the superclass (or base class).
‘‘‘java
class Animal {
void eat() {
System.out.println("Eating...");
}
}
### Overloading
Method overloading allows a class to have more than one method having the same name, if their
parameter lists are different.
‘‘‘java
class MathOperation {
int add(int a, int b) {
return a + b;
}
### Overriding
Method overriding occurs when a subclass provides a specific implementation for a method that is
already defined in its superclass.
‘‘‘java
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
### Packages
A package is a namespace that organizes classes and interfaces by functionality. Java uses packages to
avoid name conflicts and to control access.
‘‘‘java
package com.example;
Java provides a powerful mechanism for handling runtime errors using exceptions. It uses ‘try‘, ‘catch‘,
‘finally‘, ‘throw‘, and ‘throws‘.
‘‘‘java
try {
int divideByZero = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
} finally {
System.out.println("This will always execute.");
}
‘‘‘
### Threads
A thread is a lightweight process. Java provides built-in support for multithreaded programming.
‘‘‘java
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
‘‘‘java
public class Example implements Runnable {
public void run() {
System.out.println("Thread running");
}
These concepts form the foundation of Java’s Object-Oriented Programming approach and are essential
for developing robust and scalable applications.