Class 10 Java Notes
1. Revision of Class IX Syllabus
1.1 Introduction to Object-Oriented Programming (OOP) Concepts
- Object-Oriented Programming is a programming paradigm that uses objects and classes.
- Key Principles of OOP:
* Encapsulation: Wrapping data and methods in a single unit (class).
* Inheritance: Reusing code by inheriting properties from another class.
* Polymorphism: Performing one task in multiple ways (e.g., method overloading).
* Abstraction: Hiding implementation details and showing only the essentials.
1.2 Elementary Concepts of Objects and Classes
- Class: A blueprint for creating objects.
- Object: An instance of a class, representing real-world entities.
1.3 Values and Data Types
- Primitive Data Types: `int`, `float`, `char`, `boolean`, etc.
- Composite Data Types: Arrays, classes.
- Example:
```java
int num = 10; // Primitive type
String name = "Java"; // Composite type
```
1.4 Operators in Java
- Arithmetic Operators: `+`, `-`, `*`, `/`, `%`.
- Relational Operators: `<`, `>`, `<=`, `>=`, `==`, `!=`.
- Logical Operators: `&&`, `||`, `!`.
1.5 Input in Java
- Use `Scanner` class for input:
```java
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
```
1.6 Mathematical Library Methods
- `Math.sqrt(num)`: Square root.
- `Math.pow(a, b)`: a raised to the power b.
- `Math.abs(num)`: Absolute value.
1.7 Conditional Constructs
- **if-else**:
```java
if (num > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}
```
- **switch-case**:
```java
switch (day) {
case 1: System.out.println("Monday"); break;
default: System.out.println("Invalid");
}
```
1.8 Iterative Constructs (Loops)
- **for loop**:
```java
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
```
- **while loop**:
```java
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
```
1.9 Nested for Loops
- Loops inside loops:
```java
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
}
}
```
2. Class as the Basis of All Computation
2.1 Key Concepts
- Objects encapsulate:
* State: Defined by member variables (attributes).
* Behavior: Defined by member methods (functions).
- Class as Object Factory: A class is a template to create objects.
2.2 Variable Types
- Instance Variable: Belongs to an object.
- Class Variable: Shared by all objects.
- Local Variable: Declared inside a method.
2.3 Example
```java
class Student {
String name; // Instance variable
static int count = 0; // Class variable
void display() { // Method
System.out.println(name);
}
}
```
3. User-Defined Methods
3.1 Why Methods?
- Modularize code for reusability and readability.
3.2 Syntax
```java
returnType methodName(parameters) {
// body
}
```
3.3 Types
1. **Static Methods:** Accessed without creating an object.
```java
static void greet() {
System.out.println("Hello!");
}
```
2. **Non-static Methods:** Require an object to call.
```java
void greet() {
System.out.println("Hello!");
}
```
3.4 Method Overloading
- Same method name with different parameters:
```java
void add(int a, int b) { }
void add(int a, int b, int c) { }
```
4. Constructors
4.1 What is a Constructor?
- A special method that initializes objects.
4.2 Characteristics
- Same name as the class.
- No return type.
- Automatically called during object creation.
4.3 Types
1. **Default Constructor:** No parameters.
2. **Parameterized Constructor:** Takes parameters.
4.4 Example
```java
class Student {
String name;
Student(String name) {
this.name = name;
}
}
```