Java Notes
Java Notes
concepts in Java.
## Final Keyword
```java
```
```java
```
```java
```
## Static Keyword
```java
Public class Counter {
Public Counter() {
System.out.println(“Count: “ + count);
```
## Dynamic Binding
Also known as late binding, it’s the process of determining the method to
invoke at runtime rather than compile time. In Java, all method calls to
objects are resolved during runtime.
```java
Class Animal {
Void makeSound() {
System.out.println(“Animal sound”);
@Override
Void makeSound() {
System.out.println(“Bark”);
```
## Compile-time Polymorphism
```java
Class Calculator {
Return a + b;
Return a + b;
}
Return a + b + c;
```
## Runtime Polymorphism
```java
Class Shape {
Void draw() {
System.out.println(“Drawing a shape”);
@Override
Void draw() {
System.out.println(“Drawing a circle”);
}
Class Rectangle extends Shape {
@Override
Void draw() {
System.out.println(“Drawing a rectangle”);
```
## Inheritance
- Java doesn’t support multiple inheritance with classes (but does with
interfaces)
```java
// Single inheritance
Class Vehicle {
Void start() {
System.out.println(“Vehicle starting”);
Void accelerate() {
System.out.println(“Car accelerating”);
// Multilevel inheritance
Void turboBoost() {
```
## Abstract Class
```java
Void eat() {
}
Class Dog extends Animal {
@Override
Void makeSound() {
```
## Interface
```java
Interface Drawable {
System.out.println(“Drawing object”);
}
}
@Override
System.out.println(“Drawing a circle”);
```
| Methods | Can have abstract and concrete methods | All methods are
abstract by default (Java 8+ allows default and static methods) |
| Variables | Can have non-final variables | All variables are final and static by
default |
| Access Modifiers | Can use all access modifiers | All methods are implicitly
public |
## Access Modifiers
Java has four access modifiers:
```java
```
**Default vs Protected**:
- Contains JVM plus libraries and other components needed to run Java
applications
## Characteristics of OOP
```java
```
2. **Inheritance**: Mechanism where a class inherits properties and
behaviors from another class
```java
```
```java
```
```java
```
## Characteristics of Java
## Super Keyword
```java
Class Parent {
Void display() {
Parent(int value) {
This.value = value;
Child(int value) {
Void display() {
```
## Wrapper Classes
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
```java
```
## Procedural vs OOP
**Procedural Programming**:
- Examples: C, Pascal
**Object-Oriented Programming**:
- Bottom-up approach
Key differences:
## Conditional Statements
1. **If Statement**:
```java
If (condition) {
```
2. **If-Else Statement**:
```java
If (condition) {
} else {
```
3. **If-Else-If Ladder**:
```java
If (condition1) {
} else if (condition2) {
} else {
```
4. **Switch Statement**:
```java
Switch (expression) {
Case value1:
// Code
Break;
Case value2:
// Code
Break;
Default:
// Code
```
5. **Ternary Operator**:
```java
```
## Looping Statements
1. **For Loop**:
```java
// Code to be repeated
```
```java
}
```
3. **While Loop**:
```java
While (condition) {
// Code to be repeated
```
4. **Do-While Loop**:
```java
Do {
// Code to be repeated
} while (condition);
```
## Arrays
// Declaration
Int[] numbers;
// or
Int numbers[];
// Initialization
```
**Accessing Elements**:
```java
```
**Array Properties**:
**Multi-dimensional Arrays**:
```java
// Accessing elements
```
**Array Operations**:
- Passing to methods
```java
System.out.println(numbers[i]);
System.out.println(number);
```
These concepts form the foundation of Object-Oriented Programming in Java,
providing a robust framework for creating modular, reusable, and
maintainable code.