Learning Android development and Java (day1)
Tags: beginners, tutorial, java, android
Published: 2023-05-21 15:25:19

## Introduction
I am a beginner in Android development and Java. I found a great tutorial on YouTube by Derek Banas and spent three hours watching it today. Here are some key points from the tutorial that I wrote down to help me remember them. [Tutorials link](https://www.youtube.com/watch?v=fis26HvvDII)

## Java Private Variables
Java private variables are variables that cannot be accessed outside of the class in which they are declared. This can be useful for protecting sensitive data or for preventing accidental changes to variables.

To declare a private variable in Java, you can use the private keyword followed by the variable name. For example:

```
class MyClass {
  private int myVariable = 0;
}
```

The variable `myVariable `can only be accessed within the `MyClass `class. If you try to access it outside of the class, you will get an error.

## Java Inheritance

Java inheritance is a way of creating new classes that inherit the properties and methods of existing classes. This can be useful for code reuse and for creating hierarchies of classes.

To inherit from an existing class in Java, you can use the extends keyword. For example:

```
class Animal {
  public String name;

  public Animal(String name) {
    this.name = name;
  }

  public void speak() {
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {
  public Dog(String name) {
    super(name);
  }

  public void bark() {
    System.out.println("Woof!");
  }
}

Dog myDog = new Dog("Fido");
myDog.speak(); // "I am an animal"
myDog.bark(); // "Woof!"

```

In this example, the `Dog `class inherits from the `Animal` class. This means that the `Dog `class has all of the properties and methods of the `Animal `class, plus any additional properties and methods that are defined in the `Dog `class.

## Java Polymorphism
Java polymorphism is a way of allowing different objects to respond to the same message in different ways. This can be useful for code reuse and for creating flexible applications.

There are three main types of polymorphism in Java:

- **Method overloading:** Method overloading is when a class has multiple methods with the same name but different signatures. For example:

```
class MyClass {
  public void add(int x, int y) {
    System.out.println(x + y);
  }

  public void add(int x, int y, int z) {
    System.out.println(x + y + z);
  }
}

MyClass myObject = new MyClass();
myObject.add(1, 2); // 3
myObject.add(1, 2, 3); // 6

```
In this example, the `MyClass `class has two methods named `add`. The first method takes two arguments and adds them together. The second method takes three arguments and adds them together.

- **Method overriding:** Method overriding is when a subclass has a method with the same name, signature, and return type as a method in its superclass. The subclass method will override the superclass method. For example:

```
class Animal {
  public void speak() {
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {
  @Override
  public void speak() {
    System.out.println("Woof!");
  }
}

Dog myDog = new Dog();
myDog.speak(); // "Woof!"

```
In this example, the `Dog `class overrides the speak() method from the `Animal `class. The `Dog `class's `speak()` method prints "Woof!" to the console.

- **Ad hoc polymorphism:** Ad hoc polymorphism is when a function or method can be applied to different types of objects. For example:

```
class MyClass {
  public void print(Object o) {
    System.out.println(o);
  }
}

MyClass myObject = new MyClass();
myObject.print("Hello, world!"); // "Hello, world!"
myObject.print(5); // 5

```
In this example, the `print()` method can be used to print strings or numbers.

Polymorphism is a powerful tool that can be used to make Java code more concise, reusable, and flexible.