Skip to content

Commit

Permalink
[Update]: JAVA Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
omjogani committed Jan 30, 2024
1 parent e565cda commit d890cb4
Showing 1 changed file with 366 additions and 5 deletions.
371 changes: 366 additions & 5 deletions _posts/2024-01-20-java-handbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ JVM is used to convert `byte code` to `machine code`.

JVM consist of JIT (Just-In-Time) which is used to improve the performance by compiling the `byte code` to `machine code`.

---

### JRE (Java Runtime Environment)

Java Runtime Environment consists of collection of library and other components for Java Program.

It provide wide range of library to reuse the code already written.

---

### JDK (Java Development Kit)

When we download the JDK it gives JRE & JVM. It include couple of components like compiler, a debugger and other tools to develop Java Program.
Expand Down Expand Up @@ -453,7 +449,7 @@ class Vehicle {
}
}

class Bike extends Vehicel {
class Bike extends Vehicle {
public void bikeDetails(String model) {
details(model);
}
Expand All @@ -463,3 +459,368 @@ class Bike extends Vehicel {
There are several types of Inheritance available in Java.

![inheritance1](https://github.com/omjogani/blogs/assets/72139914/b4c24112-fbd0-4dd4-8f71-04128fcf46cc)

### Polymorphism

There are 2 Types of Polymorphism.

- Compiler Time Polymorphism
- Run Time Polymorphism

In Compiler Time Polymorphism, Behavior decided at compile time. eg. Method Overloading. In Run Time Polymorphism, Behavior decided at run time. eg. Method Overriding.

```java
class Vehicle {
public void move() {
System.out.println("Move");
}
}

class Bike extends Vehicle{
public void move() {
System.out.println("Bike move");
}
}
class Main {
public class main(String[] args) {
Bike bike = new Vehicle();
bike.move(); // call `move` of Vehicle Class
bike = new Bike();
bike.move(); // call `move` of Bike Class
}
}
```

We can also reassign the object as shown above and it's also called dynamic method dispatch.

## Anonymous Class

```java
class Vehicle {
public void move() {
System.out.println("Move");
}
}

class Bike {
public void details() {
System.out.println("Bike Details");
}
}
class Main {
public class main(String[] args) {
Bike bike = new Bike();
bike.details();
}
}
```

Above code can be converted to Anonymous Class.

```java
class Vehicle {
public void move() {
System.out.println("Move");
}
}

class Main {
public class main(String[] args) {
Bike bike = new Bike() {
public void details() {
System.out.println("Bike Details");
}
}
}
}
```

## Anonymous Object

```java
new Calc();
```

It only called constructor of the class `Calc`. It cannot be reuse.

```java
new Calc().show();
```

## Method Overriding

While we have Single Inheritance we can override the method in the sub class Which is extended from super class.

```java
class Vehicle {
public void move() {
System.out.println("Move");
}
}

class Bike {
public void move() {
System.out.println("Override Method");
}
}
class Main {
public class main(String[] args) {
Bike bike = new Bike();
bike.move();
}
}
```

## Packages

Combine Multiple Classes to Package.

- tools
- Calc
- AdvanceCalc
- database
- Connections
- Operations

Here `tools & database` are packages & `Calc, AdvanceCalc, Connection, Operations` are Classes.

- tools
- ui
- login
- home

Here `tools` is a package, `ui` is sub-package & `login & home` are Classes.

```java
import tools.*;
```

We can import everything from `tools` package with above lines.

## Access Modifier Table

| | Private | Protected | Public | Default |
| ------------------------------ | ------- | --------- | ------ | ------- |
| Same Class | YES | YES | YES | YES |
| Same Package Sub-Class | NO | YES | YES | YES |
| Same Package Non-SubClass | NO | YES | YES | YES |
| Different Package Sub-Class | NO | YES | YES | NO |
| Different Package Non-SubClass | NO | NO | YES | NO |

## Final Keyword

Final keyword can be used with Variables, Functions as well as Classes.

If We use final with Variables then that Variable will become **constant**.

```java
final double PI = 3.14;
```

If We use final with Methods then It will prevent **method overriding**.

```java
public final void display() {
System.out.println("I can't Override!");
}
```

If We use final with Classes then It will prevent **Inheritance**.

```java
final class MyClass {
/* ...body... */
}
```

---

If we try to print the Object it will print `ClassName@HashCode`. Because the default implementation of the `toString()` method consist of `ClassName@HashCode`.

It is possible to customize the default behavior of `toString()` by overriding `toString()` method.

We have equal method to Override and have checks based on values. By default it compare with HashCode.

## UpCasting & DownCasting

```java
class Vehicle {
public void move() {
System.out.println("Move");
}
}

class Bike {
public void details() {
System.out.println("Bike Details");
}
}
class Main {
public class main(String[] args) {
Vehicle vehicle = (Vehicle) new Bike(); // UpCasting
Bike bike = (Bike) vehicle; // DownCasting
}
}
```

## Abstract

Abstract Method is used to just declare the method. Abstract method is only used in abstract class. We can't create object of abstract class.
```java
abstract class Vehicle {
public abstract void show();
}
class Car extends Vehicle {
public void show() {
System.out.println("Show Method");
}
}
```
## Inner Class
```java
class Transport {
int num;
public void show() {
System.out.println("Transport Show");
}
// inner class
class Vehicle {
public void details() {
System.out.println("Details");
}
}
}
class Main {
public class main(String[] args) {
Transport trans = new Transport();
trans.show();
Transport.Vehicle vehicle = trans.new Vehicle();
vehicle.details();
}
}
```
## Interfaces
All the method in interface are Public & Abstract.
```java
interface Pokemon {
String name = "Pikachu";
void getAbilities();
void getHeight();
void getWeight();
}
class PokeService implements Pokemon {
/* implements all the methods of Pokemon*/
}
interface PokeDetails extends Pokemon {
void details();
}
```
Every Variable in Interface is **Final & Static**.
```java
interface Pokemon {
void getAbilities();
// default implementation: No need to implement
default void getDetails() {
System.out.println("Pokemon Details");
}
}
```
It's possible to provide default implementation of method inside interface so that It's not necessary to implement that method in implemented class.

## Enums

Enums are used to provide set of static values in Java. Enums also contain methods, constructors and variables.

```java
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public class EnumTest {
Day day;

public EnumTest(Day day) {
this.day = day;
}

public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;

case FRIDAY:
System.out.println("Fridays are better.");
break;

case SATURDAY: case SUNDAY:
System.out.println("Weekends are best.");
break;

default:
System.out.println("Midweek days are so-so.");
break;
}
}

public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
}
}
```

## Annotation

It is always good idea to use `Annotation` because...

- Code will be clean & readable
- Prevent Temporary Typos
- Some Errors can be catch at compiler time. (eg. @FunctionalInterface will give compile time error in case we have more than 1 method in interface.)

e.g.

- @Override
- @Test
- @FunctionalInterface

## Functional Interface

A interface which has exactly one method is called functional interface. It is annotated with `@FunctionalInterface`. We can use lamda function if We've used Functional Interface.

```java
@FunctionalInterface
interface PokemonOperation {
public List<Pokemon> getAllPokemons();
}

public class Main {
// with anonymous class & without lamda expression
PokemonOperation po = new PokemonOperation(){
public List<Pokemon> getAllPokemons() {
return pokemons;
}
}


// with anonymous class & lamda expression
PokemonOperation op = () -> {
retrun pokemons;
}
}
```

0 comments on commit d890cb4

Please sign in to comment.