Comprehensive Guide: Java Classes, Methods, and Functions
1. Java Classes
- A class is a blueprint from which individual objects are created.
- Syntax:
public class ClassName {
// Fields
// Constructors
// Methods
}
- Example:
public class Car {
String color;
int speed;
void drive() {
System.out.println("Driving...");
}
}
- Class types: Concrete, Abstract, Final, Static nested classes, Inner classes.
2. Objects and Object Creation
- An object is an instance of a class.
- Created using the `new` keyword.
- Example:
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
3. Constructors
- A constructor initializes an object when it is created.
- Has the same name as the class and no return type.
- Types: Default, Parameterized, Copy constructor.
- Example:
public Car(String color, int speed) {
this.color = color;
this.speed = speed;
}
Comprehensive Guide: Java Classes, Methods, and Functions
4. Methods in Java
- A method defines behavior of a class.
- Syntax:
returnType methodName(parameters) {
// body
}
- Example:
public int add(int a, int b) {
return a + b;
}
- Special types: Static methods, Instance methods, Getter/Setter methods.
5. Static Methods and Variables
- Static methods belong to the class, not instance.
- Can be accessed without creating objects.
- Static variables are shared across all objects.
- Example:
public static void showInfo() {
System.out.println("Static method called.");
}
6. Method Overloading and Overriding
- Overloading: Same method name, different parameters.
- Overriding: Subclass provides specific implementation.
- Example Overload:
int add(int a, int b)
double add(double a, double b)
- Example Override:
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
7. Access Modifiers
Comprehensive Guide: Java Classes, Methods, and Functions
- public: accessible everywhere
- private: accessible only in class
- protected: accessible in package and subclass
- default: accessible in package
- Use to enforce encapsulation and security.
8. Abstract Classes and Methods
- Abstract class cannot be instantiated.
- Abstract method must be implemented in subclasses.
- Example:
abstract class Animal {
abstract void makeSound();
}
class Dog extends Animal {
void makeSound() { System.out.println("Bark"); }
}
9. Interfaces in Java
- Interface is a blueprint of a class.
- Methods in interfaces are abstract by default.
- Support multiple inheritance.
- Example:
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() {
System.out.println("Dog eats");
}
}
10. Common Built-in Classes and Methods
- String: length(), substring(), charAt(), equals(), toUpperCase()
- Math: abs(), pow(), sqrt(), max(), min()
- Arrays: sort(), binarySearch(), fill()
Comprehensive Guide: Java Classes, Methods, and Functions
- Collections: List, Set, Map, with methods like add(), remove(), get()
- Scanner: nextLine(), nextInt(), nextDouble()
11. Final Keyword
- final variable: value cannot be changed
- final method: cannot be overridden
- final class: cannot be subclassed
- Example:
final int MAX = 100;
12. Exception Handling Methods
- try-catch-finally for handling exceptions.
- throw to explicitly throw an exception.
- throws to declare checked exceptions.
- Example:
try {
int x = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
13. Lambda Functions
- Introduced in Java 8 for functional programming.
- Syntax:
(parameters) -> expression
- Example:
List<String> list = Arrays.asList("A", "B", "C");
list.forEach(item -> System.out.println(item));
14. Functional Interfaces
- Interface with one abstract method.
- Used in lambda expressions.
- Annotated with @FunctionalInterface.
- Example:
Comprehensive Guide: Java Classes, Methods, and Functions
@FunctionalInterface
interface MyFunction {
void run();
}
15. Annotations and Reflection
- Annotations provide metadata.
- Reflection allows inspection and modification at runtime.
- Common annotations: @Override, @Deprecated, @FunctionalInterface
- Example Reflection:
Class<?> clazz = Class.forName("MyClass");
Method[] methods = clazz.getDeclaredMethods();
16. Java 8+ Enhancements in Methods
- Default methods in interfaces.
- Static methods in interfaces.
- Streams API and method references.
- Example:
interface MyInterface {
default void show() { System.out.println("Default"); }
static void log() { System.out.println("Static"); }
}